Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function squares(array) {
- // your code here
- return array.map(function(num) {return Math.pow (num, 2);});
- return num;
- }
- /* From here down, you are not expected to
- understand.... for now :)
- Nothing to see here!
- */
- // tests
- function testFunctionWorks(fn, input, expected) {
- const 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() {
- const input1 = [1, 2, 3, 4, 5];
- const result1 = [1, 4, 9, 16, 25];
- const input2 = [2, 4, 6, 8];
- const result2 = [4, 16, 36, 64];
- const testResults = [
- testFunctionWorks(squares, input1, result1),
- testFunctionWorks(squares, input2, result2),
- ];
- const numPassing = testResults.filter(function(result){ return result; }).length;
- console.log(numPassing + ' out of ' + testResults.length + ' tests passing.');
- }
- runTests();
Add Comment
Please, Sign In to add comment