I am having trouble calculating the sum of an array in my code. Instead of adding up all the numbers, it is concatenating them together. For example, if I have grades 90, 95, and 100, the sum shows as 09590100. The first FOR loop successfully pushes the grades into an array, but the second loop where I calculate the total seems to be incorrect.
var numOfGrades = prompt("How many assignments are there?");
var grades = [];
var sum = 0;
var grade = 0;
var avg = 0;
for (var i = 0; i < numOfGrades; i++) {
grade = prompt("Enter score");
grades.push(grade);
}
for (var j = 0; j < grades.length; j++) {
sum += grades[j];
}
avg = (sum / grades.length)