EASIER SOLUTION FOR GOLF CODE CHALLENGE
Javascript Algorithms and Data Structures Certification > Basic Javascript > Golf Code
My Solution:
const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
if(strokes == par && strokes > 1){
return "Par"
}
if(strokes >= 7){
return "Go Home!"
}
return names[strokes - 1];
}
golfScore(5, 4);
Given Solutions:
Solution 1
const 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) {
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 {
return names[6];
}
// Only change code above this line
}
// Change these values to test
golfScore(5, 4);
Solution 2
const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
function golfScore(par, strokes) {
return strokes === 1
? names[0]
: strokes <= par - 2
? names[1]
: strokes === par - 1
? names[2]
: strokes === par
? names[3]
: strokes === par + 1
? names[4]
: strokes === par + 2
? names[5]
: names[6];
}
Test my code with different parameters.
I’ve been trying to find out how to publish this as an official solution. Please Help.