Created
March 16, 2018 23:45
-
-
Save liseferguson/3e06f1a58ff52c4d4e12c2f43c33ec88 to your computer and use it in GitHub Desktop.
Find average
This file contains hidden or 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) { | |
| let total = numbers[0]; | |
| for (let i = 1; i < numbers.length; i++) { | |
| total += numbers[i]; | |
| } | |
| return total / 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() { | |
| 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