Golf code QA testing

I’m trying to QA a test case if a user entered “0” for either par or strokes, an error will be displayed.

var names ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"]
function golfScore (par, strokes) { 
If (par || strokes <= 0) { 
return "Please enter a value greater than 0"; 
}
else if (strokes == 1) {
return names[0];
}
//rest of solution omitted
return names[6];
}

if i pass golfScore(0,1);

it’s returning “Hole-in-one!” . Shouldn’t the if (par || strokes <= 0) break the function if either par or strokes are 0?

if i change it to if/else statements, it seems to be working correctly.

if (par <= 0) {
    return "Please enter a number greater than " + par + " for Par";
  }
else  if (strokes <= 0) {
      return "Please enter a number greater than " + strokes + " for Strokes"
    }

I see a few things that may give troubles…

line 1: missing the assignment operator var names = [...];
line 3: the if is case sensitive (it should be all lower case)
line 3: par || strokes <= 0 doesn’t work as you want: first it takes par and it evaluates if it’s true or false, then it takes strokes <= 0 and it evaluates if it’s true or false and then combine the two with the OR operator (try console.log(par || strokes <= 0)); you would need to specify par <= 0 || strokes <= 0.

for the reason above golfScore(0,1); don’t break because par in this case evaluates as false and strokes <= 0 as true, so it passes. if instead you use par <= 0 || strokes <= 0 you will get the result you expect

1 Like

thanks for suggestions. actually my source had the operator and lcase if. not sure why it changed here.

otherwise, I’ve got it to work as expected.

if (par < 1 || strokes < 1) {
if (par < 1) {
return "Please enter a value greater than " + par + " for Par";
}
else if (strokes < 1) {
return " Please enter a value greater than " + strokes + " for Strokes";
}
}
//rest of the solution...

Now the question: you have two strings that are almost identical, could you reduce the repeated code?

(The following info will may be eventually useful for you, you can mostly ignore it for now, just wanted to note that reducing strings is not always the best practice.
Note that if your strings will need to be translated the more context the translators will have the best, also it is best to have whole phrases instead of fragments, because grammatar - and this is experience as a translator for an open source project)

2 par + " for Par"; someone else.

@ToniCathey what? I’m not following your reply.