Write a javascript program that asks the user to enter up to 10 golf scores, which are to be stored in an array. You should provide a means for the user to terminate input prior to entering 10 scores. The program should display all the scores on one line and report the average score. Handle input, display, and the average calculation with three separate array processing functions
function getScores() {
var scores = [];
for(var i=0;i< 10;i++) {
scores.push(parseInt(prompt("Please enter a score "+(i+1)+": ")));
}
return scores;
}
function average(scores) {
var t= 0;
for (var i = 0; i < scores.length; i++) {
t += scores[i];
}
avrg = (t / scores.length).toFixed(2);
return avrg;
}
function printScores(scores,avrg) {
var strScores=""
for (var i = 0; i < scores.length; i++) {
strScores += scores[i]+" ";
}
alert("Scores: "+strScores+"\nThe average of the scores is: " + avrg);
}
var scores =getScores();
var avrg=average(scores);
printScores(scores,avrg);
Comments
Leave a comment