Skip to content

Instantly share code, notes, and snippets.

Created June 9, 2017 21:08
Show Gist options
  • Save anonymous/e9e4d10052e961a6a6f72b9516800e32 to your computer and use it in GitHub Desktop.
Save anonymous/e9e4d10052e961a6a6f72b9516800e32 to your computer and use it in GitHub Desktop.
computeAveDrillJS created by dtstanley - https://repl.it/G9KO/135
function average(numbers) {
// return (numbers.reduce(function sum(total, num){
// return total + num;
// })/numbers.length);
// Second way to perform with loop
var maxNum = numbers[0];
for (var i =1; i<= numbers.length -1; i++){
maxNum += numbers[i];
}
return maxNum/numbers.length;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testFunctionWorks(fn, input, expected) {
if (fn(input) === expected) {
console.log('SUCCESS: `' + fn.name + '` works on `[' + input + ']`');
return true;
}
else {
console.log(
'FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
' but was ' + fn(input)
);
return false;
}
}
(function runTests() {
var numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var correctAns1 = 5.5;
var numList2 = [0, -1, 1];
var correctAns2 = 0;
var testResults = [
testFunctionWorks(average, numList1, correctAns1),
testFunctionWorks(average, numList2, correctAns2)
];
var numPassing = testResults.filter(function(result){ return result; }).length;
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.')
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment