Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ export default function NumericQuestionAnalytics({
question,
responses,
}: Props) {
const { mean, median, mode } = useMemo(() => {
const { mean, median, mode, standardDeviation } = useMemo(() => {
let mean: number;
let median: number;
let standardDeviation: number;
let mode: number[] = [];
const numbers: number[] = [];
let total = 0;
let sdTotal = 0;
let stdMean: number;
const occurrences: number[] = [];
let maxOccur = 2;

Expand All @@ -35,6 +38,11 @@ export default function NumericQuestionAnalytics({
const number = questionResponse.answer;
total += number;
numbers.push(number);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep this :)


// calculate mean for std
stdMean = ((total / numbers.length) * 100) / 100;
sdTotal += Math.pow(number - stdMean, 2);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove line 43, and move line 44 into a separate loop

if (!occurrences[number]) occurrences[number] = 0;
occurrences[number]++;
}
Expand Down Expand Up @@ -68,10 +76,17 @@ export default function NumericQuestionAnalytics({
}
});

// calculate standard deviation
standardDeviation = Math.sqrt(sdTotal / numbers.length);

// Rounds standard deviation to hundredths place
standardDeviation = Math.round(standardDeviation * 100) / 100;

@jmcheek jmcheek Apr 25, 2025

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll want to calculate the mean here, your line 43 should work I believe ->
const mean = total / numbers.length
You then want to loop through and calculate the variance, and then the standard deviation
so, use your line 44 here
for (const num of numbers){
sdTotal += Math.pow(number - stdMean, 2)
}
then leave line 83 just as you have it outside the loop, and your done!

return {
mean,
mode,
median,
standardDeviation,
};
}, [responses]);

Expand All @@ -88,6 +103,9 @@ export default function NumericQuestionAnalytics({
Mode: {mode.length ? mode.join(', ') : 'None'}
</ListItemText>
</ListItem>
<ListItem>
<ListItemText>Standard Deviation: {standardDeviation}</ListItemText>
</ListItem>
</List>
);
}