dimanche 24 juillet 2016

Endless loop for Write a function first_n_evens(n) that returns an array of the first N many even numbers

I can't figure out how to correctly complete this question.

Write a function first_n_evens(n) that returns an array of the first N many even numbers, starting from 0.

Please help me understand what I am doing wrong.

Many Thanks

def first_n_evens(n)
array = []
number = 0

while array.count <= n
    if number % 2 == 0
        array.push(number)
        number += 1
    end
end

return array

end

test = first_n_evens(5) puts test

Aucun commentaire:

Enregistrer un commentaire