A bit of a breakdown
const names = ["Hole-in-one!", "Eagle", "Birdie", "Par", "Bogey", "Double Bogey", "Go Home!"];
Arrays are indexed starting at zero.
// names Array
0 : "Hole-in-one!",
1 : "Eagle",
2 : "Birdie",
3 : "Par",
4 : "Bogey",
5 : "Double Bogey",
6 : "Go Home!"
For example
console.log(names[0]); // "Hole-in-one!"
console.log(names[5]); // "Double Bogey"
console.log(names[9]); // undefined (nothing stored in index position 9)
We have a function called golfScore that takes two arguments, par and strokes.The function will return (output) a string e.g. “Hole-in-one!” or “Go Home!”. The string the function returns depends on the values you pass in for par and strokes.
Commented code
function golfScore(par, strokes) {
// Note: 'return' will output a value and immediately exit the function at that point.
// if strokes equals 1
if (strokes === 1) {
return names[0] // exit with "Hole-in-one!"
}
// if strokes is smaller or equals par minus 2
// e.g. golfScore(5, 2) a par of 5 minus 2 is 3, and strokes is 2.
// 2 is smaller or equal to 3, 2 <= 3 is true
else if(strokes <= par - 2) {
return names[1]; // exit with "Eagle"
}
// if strokes is equal to par minus 1.
// e.g. golfScore(4, 3) a par of 4 minus 1 is 3, and strokes is 3.
// 3 equals 3, 3 === 3 is true
else if(strokes === par - 1) {
return names[2]; // exit with "Birdie"
}
// if strokes equals par.
// e.g. golfScore(2, 2) a par of 2, and strokes is 2.
// 2 equals 2, 2 === 2 is true
else if(strokes === par) {
return names[3]; // exit with "Par"
}
// if strokes equals par plus 1.
// e.g. golfScore(2, 3) a par of 2 plus 1 is 3, and strokes is 3.
// 3 equals 3, 3 === 3 is true
else if(strokes === par + 1) {
return names[4]; // exit with "Double Bogey"
}
else if(strokes === par + 2) {
return names[5]; // exit with "Bogey"
}
// if strokes is larger than or equals par plus 3.
// e.g. golfScore(2, 7) a par of 2 plus 3 is 5, and strokes is 7.
// 7 is larger than or equal to 5, 7 >= 5 is true
else if(strokes >= par + 3){
return names[6]; // exit with "Go Home!"
}
// if none of the condition above are met then
// return with "Change Me"
else {
return "Change Me";
}
}
// calling golfScore passing in a par of 5 and strokes of 4
golfScore(5, 4); // strokes === par - 1, outputs "Birdie"
There is a comment at the end of the function // Only change code above this line
. This indicates that the code need changing/refactoring. Without knowing what the question is though, it is not possible to give an answer.