Kevin Powell - CommunityKP-C
Kevin Powell - Community2y ago
27 replies
loss

How to access the last value?

So, basically this is my code:
function getAverage(scores) {
  let value = 0;
  for (let x in scores){
    value += scores[x] / scores.length;
    console.log(value);
  };
} 

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89])) 
//right here should print 71.7


And here is the output:

18
19.2
26.9
32.6
42.6
49.300000000000004
53.1
62.8
71.7
undefined

I have two issues. First, why is giving underfined ? And everytime i change the console.log(value) to return value is not returning the others values, always end on the 9.2, so i'm kinda lost on it. Sorry if i explained bad, english is not my first language
Was this page helpful?