Could someone explain me the logic of the code?
var names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
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 {
return "Go Home!";
}
}
// Change these values to test
alert(golfScore(4,5));
I know it’s correct but could someone explain me why does “(4,5)” returns “Bogey”(as an example)? And why each number returns the expected value. I don’t seem to understand the logic. Could someone clear my mind?
I don’t seem to understand the logic like:
golfScore(4, 1)should return "Hole-in-one! golfScore(4, 2)
should return “Eagle”
golfScore(5, 2)should return "Eagle" golfScore(4, 3)
should return “Birdie”
golfScore(4, 4)` should return “Par”
I took this code from here https://guide.freecodecamp.org/certifications/javascript-algorithms-and-data-structures/basic-javascript/golf-code/