## Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

### Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

###### Pierre Grönberg

10,294 Points# How do I calculate these questions? I feel like there's no similarity from the clips.

How do I calculate these questions? I feel like there's no similarity from the clips.

## 1 Answer

###### Steven Parker

210,472 PointsThe quiz questions are created intentionally to be different from the course examples. The similarity is only in that they test your understanding of the __concepts__ introduced in the videos, but not your memory of the specific code shown in the course.

If you need help with a specific question, you could quote it here and be specific about the part causing you difficulty.

## Pierre Grönberg

10,294 Points## Pierre Grönberg

10,294 PointsHi, yeah i understood the videos but not when put like this?

## Steven Parker

210,472 Points## Steven Parker

210,472 PointsPerhaps it would help to pick one and examine it individually.

## Pierre Grönberg

10,294 Points## Pierre Grönberg

10,294 PointsWhat does the following code display? $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; } }

echo $sum;

## Steven Parker

210,472 Points## Steven Parker

210,472 PointsThat's a good one. Now without even looking at the answer choices (or actually compiling and running it!), look it over and think about how the computer would handle it. What output would you expect, or what part of the program might you be having trouble in knowing what it would do?

## Pierre Grönberg

10,294 Points## Pierre Grönberg

10,294 PointsI would see it like :

$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; } }

echo $sum;

It would start by showing it as 1234 sum of it all is 0 the loop starts at 0

theeeen im lost

## Steven Parker

210,472 Points## Steven Parker

210,472 PointsSo that's the starting conditions, except for $total. What would that have in it at the start?

Then the loop begins. Think about what things change inside the loop, and how many times the loop runs. When the loop is done, what is in $sum?

## Pierre Grönberg

10,294 Points## Pierre Grönberg

10,294 Pointsit would be 0 at the start because the $sum = 0?

then the loop goes one time because of $loop+1?

but i don't understand why the loop takes away any numbers or adds it to the $sum?

## Steven Parker

210,472 Points## Steven Parker

210,472 PointsThe value of $total would not be 0. It starts out with "

`$total = count($numbers)`

". So what would that put in it?The value of $loop does change inside the loop, but it does not determine how many times it runs. The times a "foreach" loop runs is based on the array it is working on.

Once you understand what is in $total you should be able to determine when the loop will add to $sum.

## Steven Parker

210,472 Points## Steven Parker

210,472 PointsPierre Grönberg — Did you figure it out? You can mark a question solved by choosing a "best answer".

And happy coding!