learn-co-curriculum / programming-univbasics-4-square-array

Other
0 stars 3 forks source link

What am I doing wrong? #4

Closed kendofriendo closed 5 years ago

kendofriendo commented 5 years ago
def square_array(array)
  square_bois[]
  counter = 0
    while array[counter] do
    square_bois.push(array[counter]**2)
    counter+=1
    end
    p square_bois
end

Failures:

1) #square_array does not call on each, collect, map, or inject Failure/Error: while array[counter] do square_bois[] counter = 0 square_bois.push(array[counter]**2) counter+=1 end

 NameError:
   undefined local variable or method `counter' for #<RSpec::ExampleGroups::SquareArray:0x0000000002b7fdc0>
 # ./lib/square_array.rb:2:in `square_array'
 # ./spec/square_array_spec.rb:11:in `block (2 levels) in <top (required)>'

2) #square_array should square the elements in an array Failure/Error: while array[counter] do square_bois[] counter = 0 square_bois.push(array[counter]**2) counter+=1 end

 NameError:
   undefined local variable or method `counter' for #<RSpec::ExampleGroups::SquareArray:0x0000000002c5f9e8>
 # ./lib/square_array.rb:2:in `square_array'
 # ./spec/square_array_spec.rb:15:in `block (2 levels) in <top (required)>'
kendofriendo commented 5 years ago

Disregarding the missing = after square_bois...

Here is the code I ended up with that works:

def square_array(array)
    counter = 0
    square_bois = []

    while array[counter]
    square_bois.push array[counter]**2
    counter+=1
    end
    square_bois
end

numbas= [3, 6, 9]
square_array(numbas)