carolineartz

3/9/2014 - 11:24 PM

Calculate the sum, mean, and median of an array in JavaScript

```
/*
Write THREE functions: sum, mean, and median. Each accepts an array
and returns its respective calculation.
Below you will find driver code. Run the code in this file either from the command line
using Node.js or by pasting the code of this entire file into your
browser console. All tests will log 'true' in the console when
they pass--false, otherwise.
I would encourage you to let the tests guide your work. Code has been
provided to pass the first test. The next step would be to make the
second test pass, and so on--sometimes your solution might result
in multiple tests passing.
*/
// __________________________________________
// Write your code below.
var sum = function(array) {
var total = 0;
for (var i=0; i<array.length; i++) {
total += array[i];
}
return total;
};
var mean = function(array) {
var arraySum = sum(array);
return arraySum / array.length;
};
var median = function(array) {
array = array.sort();
if (array.length % 2 === 0) { // array with even number elements
return (array[array.length/2] + array[(array.length / 2) - 1]) / 2;
}
else {
return array[(array.length - 1) / 2]; // array with odd number elements
}
};
// __________________________________________
// Reflection
/*
I don't feel all that comfortable with JavaScript. I'm trying to remember if this is how I felt about
Ruby when I was getting the hang of things...I just find that I have a plan in mind but don't know the
exact syntax to use...everytime--even for things I have already done. This is excluding for loops since
they're basically the same syntax as Java, so I can do those easily. It's mostly the way of calling functions
and the whole literal syntax and option of first class functions...it's overwhelming and hard for me to see
when to use what. jQuery seems like it will be a helpful tool, as well as learning coffeescript...but that's
after I can pin down these fundamentals...Just need more practice I guess! I've been playing with quite a bit
of things, extending the manipulating the DOM exercise...almost too much and now I feel like I've dug myself
into a deep hole because I have such big plans for the outcome. I probably shouldn't have attempted quite so
much but it is forcing me to really think about the language and understand how it integrates with the other
front-end staples with which I feel very comfortable.
*/
// __________________________________________
// Driver Code: Do not alter code below this line.
oddLengthArray = [1, 2, 3, 4, 5, 5, 7];
evenLengthArray = [4, 4, 5, 5, 6, 6, 6, 7];
function assert(test, message, test_number) {
if (!test) {
console.log(test_number + "false");
throw "ERROR: " + message;
}
console.log(test_number + "true");
return true;
}
// tests for sum
assert(
(sum instanceof Function),
"sum should be a Function.",
"1. "
);
assert(
sum(oddLengthArray) === 27,
"sum should return the sum of all elements in an array with an odd length.",
"2. "
);
assert(
sum(evenLengthArray) === 43,
"sum should return the sum of all elements in an array with an even length.",
"3. "
);
// tests for mean
assert(
(mean instanceof Function),
"mean should be a Function.",
"4. "
);
assert(
mean(oddLengthArray) === 3.857142857142857,
"mean should return the average of all elements in an array with an odd length.",
"5. "
);
assert(
mean(evenLengthArray) === 5.375,
"mean should return the average of all elements in an array with an even length.",
"6. "
);
// tests for median
assert(
(median instanceof Function),
"median should be a Function.",
"7. "
);
assert(
median(oddLengthArray) === 4,
"median should return the median value of all elements in an array with an odd length.",
"8. "
);
assert(
median(evenLengthArray) === 5.5,
"median should return the median value of all elements in an array with an even length.",
"9. "
);
```