I don’t understand why when I’m iterrating over my array it does not seem to take into account what are the element inside it…
Your code so far
// User Editable Region
function getAverage(scores) {
let sum = 0;
for (let note in scores){
sum += note
console.log(note)
}
console.log(sum)
sum = sum /scores.length
console.log(sum)
return sum
}
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
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Challenge Information:
Review JavaScript Fundamentals by Building a Gradebook App - Step 1
for..in loops are used to iterate over object properties. If you used it on arrays, it will use the index of that item on each iteration instead of its value.
const obj={
name: 'aymeric.sabr',
language: 'JS'
}
for(let item in obj){
console.log(item + ": " + obj[item]);
}
/* OUTPUT:
name: aymeric.sabr
language: JS
*/
let arr = [6, 7, 8];
for(let item in arr){
console.log(item);
console.log(arr[item]);
}
/* OUTPUT:
0 -> INDEX
6
1 -> INDEX
7
2 -> INDEX
8
*/
To use the value of an item at each iteration, use for..of loop instead.