Skip to content

Instantly share code, notes, and snippets.

@dtstanley
Forked from anonymous/squareMapJS.js
Created June 9, 2017 14:45
Show Gist options
  • Save dtstanley/11226c82ec0ee3532c8d68cc6a754cd9 to your computer and use it in GitHub Desktop.
Save dtstanley/11226c82ec0ee3532c8d68cc6a754cd9 to your computer and use it in GitHub Desktop.
squareMapJS created by dtstanley - https://repl.it/G9HI/139
function squares(array) {
return array.map(function(x){
return Math.pow(x,2);
})
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
*/
// tests
function testFunctionWorks(fn, input, expected) {
var result = fn(input);
if (
result && result.length === expected.length &&
result.every(function(item) {
return expected.indexOf(item) > -1;
})) {
console.log('SUCCESS: `' + fn.name + '` works!')
return true;
}
else {
console.error('FAILURE: `' + fn.name + '([' + input + '])` should be ' + expected +
' but was ' + fn(input))
return false;
}
}
function runTests() {
var input1 = [1, 2, 3, 4, 5];
var result1 = [1, 4, 9, 16, 25];
var input2 = [2, 4, 6, 8];
var result2 = [4, 16, 36, 64];
var testResults = [
testFunctionWorks(squares, input1, result1),
testFunctionWorks(squares, input2, result2),
];
var numPassing = testResults.filter(function(result){ return result; }).length;
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
}
runTests();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment