I had a highly productive day. I successfully wrapped up the statistics calculator project and began working on the spreadsheet project. For reference, I have included the notes on the statistics calculator here, and you can expect to see the notes on the spreadsheet project tomorrow. After saving this draft, I created a new post to record my notes. Overall, I am pleased with my accomplishments for the day.
However, despite making good progress, I still feel a bit down. I know that sometimes our emotions don’t align with our achievements, though it is still frustrating. I want to do something to cheer myself up, but I’m unsure what that is. And I certainly don’t have the time to do anything for myself at the moment. I should plan to do something this weekend when I have time.
I hate to end this post on such a down note. Alexis called me today to say hi. She mentioned having to look up monkeys for her anthropology project and how cute they are. Her friend came by her table and started talking, so I let her go so she can talk to her friends. Alex is home from his grandparents’. It is his birthday tomorrow. I wonder if we will have gluten-free cake this weekend?
JavaScript notes…
——————————–
Statistics Calculator steps 45 – 50
Step 45
Your final calculation is the standard deviation, which is the square root of the variance.
Begin by declaring a getStandardDeviation function, with the array parameter. In the function body, declare a variance variable and assign it the variance of the array.
const getStandardDeviation = (array) => { const variance = getVariance(array); }
Step 46
To calculate a root exponent, such as x√n
, you can use an inverted exponent x1/n
.
Declare a standardDeviation variable, and use the Math.pow() function to assign it the value of variance1/2
.
const getStandardDeviation = (array) => { const variance = getVariance(array); const standardDeviation = Math.pow(variance, 1/2) }
Step 47
The Math object has a .sqrt() method specifically for finding the square root of a number.
Change your standardDeviation variable to use this method instead of Math.pow().
const getStandardDeviation = (array) => { const variance = getVariance(array); const standardDeviation = Math.sqrt(variance); }
Step 48
Return your standardDeviation variable.
const getStandardDeviation = (array) => { const variance = getVariance(array); const standardDeviation = Math.sqrt(variance); return standardDeviation; }
Step 49
Now update the calculate function to include the standard deviation logic, like you did with your other functions.
const mean = getMean(numbers); const median = getMedian(numbers); const mode = getMode(numbers); const range = getRange(numbers); const variance = getVariance(numbers); const standardDeviation = getStandardDeviation(numbers); document.querySelector("#mean").textContent = mean; document.querySelector("#median").textContent = median; document.querySelector("#mode").textContent = mode; document.querySelector("#range").textContent = range; document.querySelector("#variance").textContent = variance; document.querySelector("#standardDeviation").textContent = standardDeviation;
Step 50
There is one last thing to fix. The .sort() method mutates the array it’s called on. It is generally bad practice to mutate a function parameter, which array is.
To fix this, add an empty .slice() call before your .sort() method. The empty .slice() call will make a shallow copy of the array, which you are free to mutate.
const getMedian = (array) => { const sorted = array.slice().sort((a, b) => a - b); const median = array.length % 2 === 0 ? getMean([sorted[array.length / 2], sorted[array.length / 2 - 1]]) : sorted[Math.floor(array.length / 2)]; return median; }