Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRoger Green
643 PointsI got lost.
Okay so I followed what was being said until he ran the print statement and got 2, 6, and 14. I understand what the for statement means but i don't understand how he got the sums from the for expression.
3 Answers
James Greer
1,526 PointsHey Roger,
In the example they use 'sum += many[i];' so as the loop runs through the array, it adds the values.
eg:
- i = 0 sum += many[0] 2 - (value at index 0 is 2 + sum which is 0 = 2 which is now set to the value of sum)
- i = 1 sum += many[1] 6 - (value at index 1 which is 4 + sum which is now 2 = 6)
- i = 2 sum += many[2] 14 - (value at index 2 is 8 + sum which is 6 = 14)
I hope that helps
Nicholas Grettler
1,037 PointsI was looking for a further explanation of this and this was exactly the answer I was looking for! Thank you!
James Greer
1,526 PointsNo problem guys. Glad I could help.
Roger Green
643 PointsRoger Green
643 PointsYou're a genius my friend thank you for your help.