Skip to content

Instantly share code, notes, and snippets.

@dtstanley
Forked from anonymous/computeAveDrillJS.js
Created June 9, 2017 20:45
Show Gist options
  • Save dtstanley/b163cd8f6abe9ab36c0fecfcfad2fceb to your computer and use it in GitHub Desktop.
Save dtstanley/b163cd8f6abe9ab36c0fecfcfad2fceb to your computer and use it in GitHub Desktop.
computeAveDrillJS created by dtstanley - https://repl.it/G9KO/126
function average(numbers) {
return (numbers.reduce(function sum(total, num){
return total + num;
})/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