 # How does this return 3?

I need a diagram. this doesn’t make any sense to me

So you’re starting with 4 variables.

``````let scoreOfAlan = 5;
let scoreOfJudy = 3;
let foulsCommittedByJudy = 1;
let foulsCommittedByAlan = 2;
``````

Then, the condition on the first `if` is true, because `foulsCommittedByJudy` has a value of 1 which is bigger than 0:

``````if (foulsCommittedByJudy > 0) // condition is true
``````

Which means the line of code inside the `if` is executed:

``````scoreOfJudy = scoreOfJudy - foulsCommittedByJudy;
``````

In this line, `scoreOfJudy` is set with a new value; Which is the current value (3) minus the value of `foulsCommittedByJudy` (1). So the new value of `scoreOfJudy` is 2.

And like in the first `if`, the condition on the second one is true, because `foulsCommittedByAlan` has a value of 2 which is bigger than 0:

``````if (foulsCommittedByAlan > 0) // condition is true
``````

Which means the line of code inside this `if` is executed as well:

``````scoreOfAlan = scoreOfAlan - foulsCommittedByAlan;
``````

`scoreOfAlan` is set with a new value; Which is the current value (5) minus the value of `foulsCommittedByAlan` (2). So the new value of `scoreOfAlan` is 3.

2 Likes

the if block resets the value of scoreOfAlan and then executes with the new value?
if the question was looking for the number 2, the answer would be scoreOfJudy?
if I am understanding the logic correctly?

The `if` block doesn’t reset the value; It checks for a condition (which is inside parentheses). Then, if the condition is true (or truthy), the line of code inside the `if` block is executed (between the curly brackets). If the condition is false (or falsy), the statement is not executed. So in our case:

``````if (foulsCommittedByJudy > 0) {
scoreOfJudy = scoreOfJudy - foulsCommittedByJudy;
}
``````

The condition inside the parentheses is like a question. It asks: “does the current value of `committedByJudy` is bigger than 0?”
Because the value of `committedByJudy` is `1`, the condition evaluates as `true`; and the line inside the `if` block is executed. in our case it’s `scoreOfJudy = scoreOfJudy - foulsCommittedByJudy;`.

This line is the one that sets a new value to `scoreOfJudy`, Which is the current value ( 3 ) minus the value of `foulsCommittedByJudy` ( 1 ). So the new value of `scoreOfJudy` is 2.

The same goes for the second `if` block. So yes, if the question was looking for the number 2, the answer would be `scoreOfJudy`.

For more info on `if...else` statements, I recommend maybe repeating the lesson on Codecademy, or check out MDN.

1 Like