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 input a score "+(i+1)+": ")));
}
return scores;
}
function calculateAverage(scores) {
var total = 0;
for (var i = 0; i < scores.length; i++) {
total += scores[i];
}
average = (total / scores.length).toFixed(2);
return average;
}
function showScores(scores,average) {
var allScores=""
for (var i = 0; i < scores.length; i++) {
allScores += scores[i]+" ";
}
alert("All scores: "+allScores+"\nThe average of the scores is: " + average);
}
var scores =getScores();
var average=calculateAverage(scores);
showScores(scores,average);
Comments
Leave a comment