How to access the last value?
So, basically this is my code:
And here is the output:
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
you're getting undefined because you're not returning anything from
getAverage
oh right, i was putting the return inside the for in loop instead
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
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?
ooooh
okay
so is working but is not right in this manner
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
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
?
not really, no. You have to sum everything, and then divide by the total length
okay, i'll write that
thanks, jochem, always saving me 🙌
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
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