Tell us what’s happening:
--------PROBLEM STATEMENT--------
A teacher has finished grading their students’ tests and needs your help to calculate the average score for the class.
Complete the getAverage function which takes in an array of test scores and returns the average score.
The average is calculated by adding up all the scores and dividing by the total number of scores.
Example Code
average = sum of all scores / total number of scores
A couple of function calls have been provided for you so you can test out your code.
Tips
You can use a loop to iterate over the scores array and add up all the scores.
You can use the length property to get the total number of scores.
------------CONSOLE--------------
// running tests
getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]) should return 71.7.
getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]) should return 85.4.
getAverage([38, 99, 87, 100, 100, 100, 100, 100, 100, 100]) should return 92.4.
Your getAverage function should return the average score of the test scores.
// tests completed
// console output
9.2
4.5
Your code so far
// User Editable Region
function getAverage(scores) {
for(let i = 0; i <= scores.length; i++){
let sum = 0;
sum += scores[i];
let avg = sum / scores.length;
return avg;
}
}
console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]));
console.log(getAverage([45, 87, 98, 100, 86, 94, 67, 88, 94, 95]));
// User Editable Region
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1