Learn Advanced Array Methods by Building a Statistics Calculator - Step 43

Tell us what’s happening:

Hello everyone,
Is it correct to make sure that we are at the last index of the array and to get the average:
const getVariance = (array) => {
const mean = getMean(array);
const variance = array.reduce((acc, el) => {
const difference = el - mean
const squared = difference ** 2
acc + squared
squared[array.length -1]
? squared[array.length -1] / array.length
: squared
},0);

return variance;
}

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

Learn Advanced Array Methods by Building a Statistics Calculator - Step 43

Yes, it is correct to make sure that you are at the last index of the array before getting the average. This is because the average of an array is calculated by adding up all of the elements in the array and dividing by the number of elements. If you are not at the last index of the array, you will not be able to include all of the elements in the calculation.

1 Like

robheyays,
Thanks for your reply. So, why is it not working now, is the syntax wrong? or what !!

1 Like

I ran the code the closest answer was return (code)/variance but not sure about that. You might change something else for this to work

It looks unsolvable to me. Can anyone help solving this challange?

Add /array.length after the .reduce()

Mod edit: removed

2 Likes

twghydc4b01,
Thanks for your help, it works

@twghydc4b01

Please do not provide code to pass the challenges

Thanks

3 Likes

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.