Javascript Basics / The golf Challenge

In this else if statement’s comparison part else if(stroke <= par - 2)
what is the value of var? That is confusing cuz below when we call the function and pass values
golfScore(5, 4); Here the value is clear

but here it’s not clear how it is being calculated.
stroke <= par - 2
stroke <= par is clear but what is " - 2 " or " +2 " in here?

function golfScore(par, strokes) {
else if(stroke <= par - 2) {
 // the code 

// Change these values to test
golfScore(5, 4);

First of all be careful to write strokes everywhere not “stroke”

Inside the if comparison you have a basic little calculation. For example in the first case we have golfScore(5, 4) . This means the “par” is 5. And "strokes’ is 4 . That means the golf course expects a good golfer to sink the ball with 5 whacks of a club (strokes). But in this example he only took 4 strokes. So he is “1 under par” And the formula looks like: Strokes = Par -1 (4 = 5 - 1).

Does this help?

BTW don’t confuse var and par.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

Note: Backticks are not single quotes.


1 Like

Yes. Thank you for explanation. I have never seen doing arithmetic with values in the if else statements. That was confusing.

Okay Ariel. I am new to FCC forums. it’s good that people help here if you get stuck but always trying to solve on my own.
And thank you!

We’re glad to have you here.

1 Like