Skip to content

Instantly share code, notes, and snippets.

@willwalker753
Created February 20, 2020 01:42
Show Gist options
  • Save willwalker753/dd786f534de63f9eb6d630cf3f14e3cc to your computer and use it in GitHub Desktop.
Save willwalker753/dd786f534de63f9eb6d630cf3f14e3cc to your computer and use it in GitHub Desktop.
average drill
function average(numbers) {
let sum = numbers[0];
for (i=1; i<numbers.length; i++) {
sum += numbers[i];
}
let average = sum / numbers.length;
return average;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
let numbers = [1,2,3];
console.log(average(numbers));
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() {
const numList1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const correctAns1 = 5.5;
const numList2 = [0, -1, 1];
const correctAns2 = 0;
const testResults = [
testFunctionWorks(average, numList1, correctAns1),
testFunctionWorks(average, numList2, correctAns2),
];
const 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