Skip to content

Instantly share code, notes, and snippets.

@AKCodez
Created October 22, 2018 20:32
Show Gist options
  • Select an option

  • Save AKCodez/378f31ce306f1b089b470930bff0fba9 to your computer and use it in GitHub Desktop.

Select an option

Save AKCodez/378f31ce306f1b089b470930bff0fba9 to your computer and use it in GitHub Desktop.
Module 2 #11
/*
Use the skeleton provided to write a working implementation.
Notes:
* Do not leave any functions in the skeleton unused.
* Test that your implementation works.
Specifically -- your code reviewer should be able to just press the [Run] button and see convincing evidence that your code works, because of:
a) the categorical reasoning displayed by your tests
b) the line(s) of output in the console log saying "passed" coming from those tests
*/
// Skeleton
var sums = 0;
var numbers = [1, 2, 3, 4];
function sum(numbers) {
for(var i = 0; i < numbers.length; i++){
sums += numbers[i];
}
return sums;
}
function average(numbers) {
var avg = sums / numbers.length;
return avg;
}
function assertEqual(actual, expected, testName){
if(actual === expected){
console.log("passed");
} else {
console.log("FAILED [" + testName + "] Expected " + expected + ", but got " + actual);
}
}
assertEqual(sum(numbers), 10, "sum of digits in an array");
assertEqual(average(numbers), 2.5, "average of digits in an array");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment