In: Computer Science
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.
<!DOCTYPE html> <html> <body> <h2>Golf Score Calculator.</h2> <script> function readScores() { var scores = [] for(var i = 0; i<10; i++) { var x = parseInt(prompt('Enter a golf score (-1 to exit): ')) if(x == -1) { break; } scores.push(x) } return scores; } function findAvg(scores){ var sum = 0 for(var i = 0; i<scores.length; i++) { sum += scores[i] } return sum/scores.length; } function displayResult(scores) { var result = 'Gold Scores: ' + scores.join(', ') + '\n' result += 'Avg: ' + findAvg(scores) alert(result); } displayResult(readScores()) </script> </body> </html>
************************************************** Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.
Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.