Difficult to make a meaningful post title, what I mean is I am doing a for…of exercise finding the average of an array.
This version works…
const marks = [80, 80, 50];
console.log(calculateGrade(marks));
function calculateGrade(marks) {
let sum = 0;
for (let mark of marks)
sum += mark;
let average = sum / marks.length;
if (average < 60) return 'F';
if (average < 70) return 'D';
if (average < 80) return 'C';
if (average < 90) return 'B';
return 'A';
}
but this version (where I left unnecessary curly braces in) returns ‘F’ and I can’t figure out why. I know I can just remove the braces but I am curious why it gives a wrong answer as opposed to just an error. Any advice greatly appreciated.
const marks = [80, 80, 50];
console.log(calculateGrade(marks));
function calculateGrade(marks) {
let sum = 0;
for (let mark of marks) {
sum += mark;
let average = sum / marks.length;
if (average < 60) return 'F';
if (average < 70) return 'D';
if (average < 80) return 'C';
if (average < 90) return 'B';
return 'A';
}
}