csb00 :
I am creating an array by imputing 5 different scores. Now, I am suppose to return the highest score from that particular array. However, after I am done imputting my last score, I am getting the following message: "Highest score: undefined"
Can someone point out as to why this is occuring and help me out? I am currently learning JS.
var arrScores = []; //I created an empty array where my scores will be stored.
//Here I am creating a loop so that the user can input 5 scores
for (var i = 0; i < 5; i++) {
arrScores.push(prompt('Please enter your score ' + (i+1)));
}
//I created a function thinking I would be able to pull the largest score.
function highestScore(arr){
highestScore = Math.max(arrScores)
}
//This is suppose to be the final alert with the highest score.
alert('Highest score: ' + highestScore.join);
Frustrated Programmer :
Highest score is a function. first you will need to call it
alert('Highest score: ' + highestScore(arrScores));
And to get the value you will need to return the value from the highestScore function
function highestScore(arrScores){
highestScore = Math.max(...arrScores)
return highestScore
}
Shorter method
function highestScore(arrScores){
return Math.max(...arrScores)
}