Reputation: 1155
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);
Upvotes: 2
Views: 52
Reputation: 10604
There are couple of small mistakes in the code.
highestScore(<parameter>)
Math.max
highestScore
function.I have made few changes in the code, try the below code.
Try this.
var arrScores = [];
for (var i = 0; i < 5; i++) {
arrScores.push(prompt('Please enter your score ' + (i+1)));
}
function highestScore(arr){
return Math.max(...arr);
}
alert('Highest score: ' + highestScore(arrScores));
Upvotes: 2
Reputation: 341
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)
}
Upvotes: 0