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 trialSeth Johnson
15,200 PointsI've hit the wall of incomprehension.
Okay, I seriously need help here. I plugged the code in for this portion of the quiz to my own local machine, and so got the correct answer. BUT.
I absolutely have no [expletive deleted] idea HOW or WHY.
I took things as incrementally as I feel like I possibly could have, and even went so far as to var_dump everything and make notes to myself so I could figure out what was (or wasn't) happening. Like so:
<?php
$numbers = array(1,2,3,4);
$total = count($numbers);
$sum = 0;
$loop = 0;
foreach($numbers as $number) {
$loop = $loop + 1;
if ($loop < $total) {
$sum = $sum + $number;
}
}
var_dump($number);
echo " <--- this one is 'number'" . "</br>";
var_dump($loop);
echo " <--- this one is 'loop'" . "</br>";
var_dump($loop + 1);
echo " <--- this one is 'loop plus one'" . "</br>";
var_dump($total);
echo " <--- this one is 'total'" . "</br>";
var_dump($sum);
echo " <--- this one is 'sum'" . "</br>";
var_dump($sum + $total);
echo " <--- this one is 'sum plus total.' Allegedly." . "</br>";
//echo $sum;
And it all checks out, and got all of this as my output:
- int(4) <--- this one is 'number'
- int(4) <--- this one is 'loop'
- int(5) <--- this one is 'loop plus one'
- int(4) <--- this one is 'total'
- int(6) <--- this one is 'sum'
- int(10) <--- this one is 'sum plus total.' Allegedly.
I'm certainly no mathemagician, but if my basic arithmetic is correct, "$sum" starts out as 0, and remains at 0 once the loop runs...and $number is 4 once the loop runs...so...how do the laws of time and space bend so that 0 + 4 = 6?!? (Since, y'know, that's presumably what the "$sum" + "$number" adds up to for the "correct" answer.) My brain is melting. Please advise. Thanks very much in advance.
1 Answer
Steven Parker
231,269 PointsThe second part of your premise "$sum ... remains at 0 once the loop runs" is incorrect because of this line:
$sum = $sum + $number;
Each time through the loop, $sum gets bigger:
- when $number is 1, $sum becomes 1 (0 + 1)
- when $number is 2, $sum becomes 3 (1 + 2)
- when $number is 3, $sum becomes 6 (3 + 3)
- when $number is 4, nothing happens since 6 is not less than 4 ($total)
So when the loop finishes, $sum is 6 — and the laws of time and space remain intact.
Seth Johnson
15,200 PointsSeth Johnson
15,200 PointsSomewhere off in the distance, you may hear something resembling the most tumultuous thunderstorm that ever was or ever will be...but it is, in fact, only the sound of my palm hitting my forehead, upon figuring out the situation with this loop. Thank you @Steven. :)
Steven Parker
231,269 PointsSteven Parker
231,269 PointsGlad I could help, and happy coding!