Skip to content

Instantly share code, notes, and snippets.

@rjbernaldo
Forked from JeuelyFish/gc1_refactored.js
Last active August 29, 2015 14:00
Show Gist options
  • Save rjbernaldo/11029591 to your computer and use it in GitHub Desktop.
Save rjbernaldo/11029591 to your computer and use it in GitHub Desktop.
// User stories for Group Project.
// 1. I want a program that can take any number of integers and add them together
// 2. I want a program that can take any number of integers and calculate their mean (average).
// 3. I want a program that can take any number of integers and find their median number.
// HINT: The name of these "programs" should be: sum, mean, median.
//---------------------
//PseudoCode
//1. make variable named "sum", take array of integers, produce sum of total.
//2. make variable name "mean", take array of integers, take sum of integers, divide by # of integers
//3. make variable named "median", IF even -> take array of integers, take # of intergers, divide by 2, call result, use result as position in original array to find median.
//If odd -> take array of integers, take # of intergers, divide by 2, call result, round up on result, use result as position in original array to find median.
//--------------------
//1.
function sum(collection) {
total_sum = 0;
for (var i = 0; i < collection.length; i++) {
total_sum += collection[i];
}
return total_sum;
}
//2.
function mean(collection) {
total_sum = 0;
for (var i = 0; i < collection.length; i++) {
total_sum += collection[i];
}
return total_sum / collection.length;
}
//3.
function median(collection) {
collection.sort();
var tempNum = Math.round(collection.length / 2);
if (collection.length % 2 !== 0) {
return collection[tempNum - 1];
} else {
var average = (collection[tempNum] + collection[tempNum - 1]) / 2;
return average;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment