Review JavaScript Fundamentals by Building a Gradebook App - Step 1

Tell us what’s happening:

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.

Thanks a lot, I’m pretty used to Python and I’m trying to learn JS that’s why I have done this mistake I think.
Have a nice day !

1 Like

Im looking for a career in front end development.

ok so I solved the problem by copying the code in this post. Is that the right way to solve it?

I was completely lost on how to solve this i didnt have any code written of my own.

I don’t think so. You could open a new thread for your issue and try to explain the issues you encountered or what you don’t understand.

However, if you now understand the code, try to write it again on your own or find a shorter solution, if possible.