Golf Code using ++ and --

Hi all! I wanted to solve the Golf code exercise using ++ and – but they seem to crash code: i reached the solution but only using + 1 and -1

here s what Im talking about :


var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
  // Only change code below this line
  if ( strokes == 1){
    // "Hole-in-one!"
    return names[0];
  } else if ( strokes <= par - 2){
    // "Eagle"
    return names[1];
  } else if ( strokes <= par - 1){
    // "Birdie"
    return names[2];
  } else if ( strokes == par ){
    // "Par"
    return names[3];
  } else if (  ++par == strokes){
    // "Bogey"
    return names[4];
  } else if ( strokes == par + 2){
    // "Double Bogey"
    return names[5];
  } else {
    // "Go Home!"
    return names[6];
  }
  // Only change code above this line
}

I used ++par and par++ but they dont work :frowning:

General tip: You don’t usually see people modifying the a variable involved a chain of conditions like this - it makes the logic hard to debug.

This is exactly what I mean. You are increasing par in your Bogey check and now your are effectively checking strokes == "original_value_of_par + 1" + 2 for Double Bogey.

You are making a debugging landmine by using par++ like this.

you mean I cant use ++ (or --) in this exercise becaus its an assignement?

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.

markdown_Forums

Right. Using ++ or -- changes the value of the thing you are incrementing/decrementing.

i++ is not the same as i + 1. It’s the same as i = i + 1.

1 Like