18. Blocks




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

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 !


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


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 !


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