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
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:
9.2
18
19.2
26.9
32.6
42.6
49.300000000000004
53.1
62.8
71.7
undefined
9.2
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
12 Replies
Jochem
Jochem•5mo ago
you're getting undefined because you're not returning anything from getAverage
loss
lossOP•5mo ago
oh right, i was putting the return inside the for in loop instead
Jochem
Jochem•5mo ago
and you're getting unexpected results from replacing console.log with return because you'd be returning in the for loop. Your logic in the for loop is wrong too you're adding 92 to the value, then dividing by 10, then adding 88 to the value (now 9.2+88 = 97.2) and dividing that by 10, and so on and so forth
loss
lossOP•5mo ago
function getAverage(scores) {
let value = 0;
for (let x in scores){
value += scores[x] / scores.length;
};
return value;
}

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]));
function getAverage(scores) {
let value = 0;
for (let x in scores){
value += scores[x] / scores.length;
};
return value;
}

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]));
Noow i just passed the exercise but can you explain more ? i'm curious so i should get first the sum then divid by the length of the array?
Jochem
Jochem•5mo ago
function getAverage(scores) {
let value = 0;
for (let x in scores){
value += scores[x];
};
return value / scores.length;
}

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]))
function getAverage(scores) {
let value = 0;
for (let x in scores){
value += scores[x];
};
return value / scores.length;
}

console.log(getAverage([92, 88, 12, 77, 57, 100, 67, 38, 97, 89]))
loss
lossOP•5mo ago
ooooh okay so is working but is not right in this manner
Jochem
Jochem•5mo ago
that's how you calculate an average, yeah. Sum of the values divided by the number of values it's running, but it's not calculating the average afaik
loss
lossOP•5mo ago
got it Is possible to like make such as in math where you can isolate first what you wanna to be running ? Instead of making two lines like can i make this on one line, all the math or is always better to break into smaller pieces ? idk if is possible but like return (value+= scores[x]) / scores.length something like that ?
Jochem
Jochem•5mo ago
not really, no. You have to sum everything, and then divide by the total length
loss
lossOP•5mo ago
okay, i'll write that thanks, jochem, always saving me 🙌
Jochem
Jochem•5mo ago
the for loop executes as many times as there are elements in the array, so you can't put anything in there that should only run once
loss
lossOP•5mo ago
hmm got it, since i only need to / in the end, i would just need the end result instead of always calculating over and over again k got it
Want results from more Discord servers?
Add your server