Skip to content

Instantly share code, notes, and snippets.

@user24
Created August 22, 2012 17:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save user24/3427621 to your computer and use it in GitHub Desktop.
Save user24/3427621 to your computer and use it in GitHub Desktop.
facebook.js
var rawAnswers = [38,264,35];
// Truncate eg 12345 to 12 to account for data errors.
var answers = [];
rawAnswers.forEach(function(e, i, c) {
answers.push(parseInt(e.toString().substring(0,2)));
});
var correct=0,sum=0,b=[], mode='', maxi=0;
answers.forEach(function(e) {
// Find correct answers:
if(e==40) {
correct++;
}
// For average
sum+=e;
// Find mode
if(b[e]) {
b[e]++;
} else {
b[e]=1;
}
if(maxi<b[e]) {
mode=e;
maxi=b[e];
}
});
answers = answers.sort();
console.log("Num: "+answers.length+", Correct:"+correct+" ("+Math.round(correct/answers.length*100)+"%), Min: "+answers[0]+", Max: "+answers[answers.length-1]+", Mode: "+mode+", Avg: "+Math.round(sum/answers.length));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment