-
-
Save dtstanley/4284f00324f5dc5834dbd3fd59d9f01a to your computer and use it in GitHub Desktop.
computeAveDrillJS created by dtstanley - https://repl.it/G9KO/135
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function average(numbers) { | |
//Performing task without looping: | |
// 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