Basic JavaScript: Golf code

Subscribe to my newsletter and never miss my upcoming articles

Exercise:

In the game of golf each hole has a par, meaning the average number of strokes a golfer is expected to make in order to sink the ball in a hole to complete the play. Depending on how far above or below par your strokes are, there is a different nickname.

Your function will be passed par and strokes arguments. Return the correct string according to this table which lists the strokes in order of priority; top (highest) to bottom (lowest):

Strokes            Return
1                  "Hole-in-one!"
<= par - 2         "Eagle"
par - 1            "Birdie"
par                "Par"
par + 1            "Bogey"
par + 2            "Double Bogey"
>= par + 3         "Go Home!"

par and strokes will always be numeric and positive. We have added an array of all the names for your convenience.

var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];

Solution to the question

We will make use of if else and else if conditional statements

function golfScore(par, strokes) {
  if(strokes===1){
    return names[0]; 
  }else if(strokes<=par-2){
    return names[1];
  }else if(strokes===par-1){
    return names[2];
  }else if(strokes===par){
    return names[3];
  }else if(strokes===par+1){
    return names[4];
  }else if(strokes===par+2){
    return names[5];
  }else if(strokes>=par+3){
    return names[6];
  }else{
    return "Change Me";
  }
}

Calling the function will yield these results.

GolfCode correctAnswer1.jpg

Assuming they didn't create the array of names, and we didn't want to create our own array, we could choose to write our return statements like this

function golfScore(par, strokes) {
  if(strokes===1){
    return "Hole-in-one!"; 
  }else if(strokes<=par-2){
    return "Eagle";
  }else if(strokes===par-1){
    return "Birdie";
  }else if(strokes===par){
    return "Par";
  }else if(strokes===par+1){
    return "Bogey";
  }else if(strokes===par+2){
    return "Double Bogey";
  }else if(strokes>=par+3){
    return "Go Home!";
  }else{
    return "Change Me";
  }
}

Calling the function will yield these results

GolfCode correctAnswer2.jpg

Remember that we can't return in array bracket notation here, because we didn't create the array, else it will throw an error. What type of operand is names? Answer is undefined 😉

GolfCode consoleError.jpg

We could also use switch statements to get this right, but let's dedicate another article to compare and contrast between if else, else if and switch statements.

A wrong answer that made me not to pass the test

It was actually an omission, lol

function golfScore(par, strokes) {
  if(strokes===1){
    return names[0]; 
  }else if(strokes<=par-2){
    return names[1];
  }else if(strokes===par-1){
    return names[2];
  }else if(strokes===par){
    return names[3];
  }else if(strokes===par+1){
    return names[4];
  }else if(par+2){
    return names[5];
  }else if(strokes>=par+3){
    return names[6];
  }else{
    return "Change Me";
  }
}

This is what it looks like in the console

GolfCode wrongAnswer.jpg

From this wrong answer, we can learn that everything will end at par + 2 because there will always be a truthy when it gets there. par + 2 will definitely be greater than zero (0). Any number greater than 0 is true, so the if statement (of par + 2)will definitely be executed, hence, will never get to the remaining two conditions.

I would love to see your comments or questions. Thank you very much for reading this article.

Comments (1)

Favourite Jome's photo

It's great you could figure out what your mistake was. Little mistakes like that really does take time to figure out.