For this challenge I am skeptical of how I arrived to my solution, though I did technically pass. All the other solutions offered in the hint thread differ from mine, making me fear I am missing something.
my solution:
var total = 0;
for (total; total < 20; total += myArr[0]) { }
Can you clarify what “work in the head of the loop” means so I don’t repeat that mistake?
Also, I’m unsure how I hard coded the answer of 20; I’ve declared my variable with value = 0. My condition within my loop simply stops iterations if total is less than 20. If I modify the value of expression b or expression c within my loop, I fail the challenge.
Can you unpack either of those a bit further for me please?
The for loop ‘head’ should do exactly three things
initialize an iteration variable
set an exit condition based upon the iteration variable
set how the iteration variable gets update
The solution for this challenge should say ‘start at the first array entry, continue through all array entries, incrementing one by one’ with a body that says ‘add the current array element to total’.
Your loop says ‘keep adding the first element to the total until we reach 20, the value I knew ahead of time was the answer’.
If you change the order of the contents in arr, would you get the same answer? Hint: you won’t. This is a big problem.