18. Blocks


#1



https://www.codecademy.com/courses/ruby-beginner-en-ET4bU/4/3?curriculum_id=5059f8619189a5000201fbcb#


Hi,

So i'm kind of lost with this exercise. I found the correct answer by using the hints, but i just don't understand why we must use "Sum = 2".

My code looks like this :


my_array = [1, 2, 3, 4, 5]
sum = 2

my_array.each do |n|
    sum = n*n
    puts sum
end

And i found out it was correct but... why sum = 2 on line 2 ? I get why sum = n*n since i needed to multiple each value with itself but i'm really lost about the line 2. Does someone have an explanation ?

Thanks a lot !


#2

Try deleting it and see if it makes a difference before you ask about why it's required


#3

I did and it does when i did it the first time (i got an error message). Now, it doesn't. It does'nt make any sense to me but hey... i guess it was a bug then.

Thanks anyway !


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.