Created
March 15, 2018 00:31
-
-
Save liseferguson/67128426e7ea11a5a7b2a6db1606c823 to your computer and use it in GitHub Desktop.
array copying 1
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 firstFourItems(array) { | |
| return array.slice(0, 4); | |
| } | |
| function lastThreeItems(array) { | |
| return array.slice(-3); | |
| } | |
| /* 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 + '` is not working'); | |
| return false; | |
| } | |
| } | |
| function runTests() { | |
| const list = ['red bull', 'monster', 'amp', 'rockstar', 'full throttle']; | |
| const result1 = ['red bull', 'monster', 'amp', 'rockstar']; | |
| const result2 = ['amp', 'rockstar', 'full throttle']; | |
| const testResults = [ | |
| testFunctionWorks(firstFourItems, list, result1), | |
| testFunctionWorks(lastThreeItems, list, result2), | |
| ]; | |
| const 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
https://repl.it/@liseferguson/Array-copying-I-drill