Skip to content

Instantly share code, notes, and snippets.

@ferreiro
Last active August 29, 2015 14:27
Show Gist options
  • Save ferreiro/c0ea99fce8ec97f8ebc1 to your computer and use it in GitHub Desktop.
Save ferreiro/c0ea99fce8ec97f8ebc1 to your computer and use it in GitHub Desktop.
Cheat mathematics: Simple programs to make my "Probabilty and staditics" problems for the university.
// Calculate the mean of a set.
var i;
var result = 0;
var data = [1, 2, 3, 4, 5, 6, 7, 8, 9]; // age.
var absFrec = [9, 8, 7, 6, 5, 4, 3, 2, 1]; // absolute frecuency.
var N = 0; // Total value of the set.
for (i = 0; i < data.length; i++) {
N += absFrec[i];
result += (data[i] * absFrec[i]);
}
result = Math.floor(result / N).toFixed(5);
console.log("The mean for your data is " + result);
// Calculate the mean of a set.
var j = 0;
var medianValue = N/2; // Absolute frecuency of the median
var found = false;
var accAbsFrec = 0;
while (j < absFrec.length && !found) {
accAbsFrec += absFrec[j]; // Calculate acc abs frecuncy.
if (accAbsFrec <= medianValue) {
j++;
}
else {
found = true;
}
}
console.log("N/2 = " + medianValue);
console.log("The median for your values is " + data[j] );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment