Created
April 29, 2018 22:33
-
-
Save ukhlivanov/a6d63f2e04cfd3904cc03f851c46c5c8 to your computer and use it in GitHub Desktop.
ThinkFul-Arrays
This file contains 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 accessFirstItem(array) { | |
// your code goes here | |
return array[0]; | |
} | |
function accessThirdItem(array) { | |
// your code goes here | |
return array[2]; | |
} | |
/* 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() { | |
var list = [1, 4, 9, 16, 25]; | |
var item1 = 1; | |
var item2 = 9; | |
var testResults = [ | |
testFunctionWorks(accessFirstItem, list, item1), | |
testFunctionWorks(accessThirdItem, list, item2), | |
]; | |
var numPassing = testResults.filter(function(result) { | |
return result; | |
}).length; | |
console.log(numPassing + ' out of ' + testResults.length + ' tests passing.'); | |
} | |
runTests(); |
This file contains 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 addToList(list, item) { | |
// your code goes here | |
list.push(item); | |
return list; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testAddToList() { | |
const input1 = ['red', 'blue', 'green']; | |
const input2 = 'pink'; | |
const expected = ['red', 'blue', 'green', 'pink']; | |
const result = addToList(input1, input2); | |
if ( | |
result && | |
result.length && | |
expected.length === result.length && | |
expected.every(function(item) { | |
return result.indexOf(item) > -1; | |
}) | |
) { | |
console.log('SUCCESS: `addToList` works!'); | |
} else { | |
console.error('FAILURE: `addToList` is not working'); | |
} | |
} | |
testAddToList(); |
This file contains 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 makeList(item1, item2, item3) { | |
// your code here | |
const arr=[item1,item2,item3]; | |
return arr; | |
} | |
/* From here down, you are not expected to | |
understand.... for now :) | |
Nothing to see here! | |
*/ | |
// tests | |
function testMakeList() { | |
const items = ['prime rib', 'fried goat cheese salad', 'fish tacos']; | |
const result = makeList(items[0], items[1], items[2]); | |
if ( | |
result && | |
result.length && | |
items.length === result.length && | |
items.every(function(item) { | |
return result.indexOf(item) > -1; | |
}) | |
) { | |
console.log('SUCCESS: `makeList` works!'); | |
} else { | |
console.error('FAILURE: `makeList` is not working'); | |
} | |
} | |
testMakeList(); |
This file contains 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 findLength(array) { | |
// your code goes here | |
return array.length; | |
} | |
function accessLastItem(array) { | |
// your code goes here | |
return array[array.length-1] | |
} | |
/* 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.error( | |
'FAILURE: `' + | |
fn.name + | |
'([' + | |
input + | |
'])` should be ' + | |
expected + | |
' but was ' + | |
fn(input) | |
); | |
return false; | |
} | |
} | |
function runTests() { | |
const list = [1, 4, 9, 16, 25]; | |
const originalList = [1, 4, 9, 16, 25]; | |
const length = 5; | |
const lastItem = 25; | |
const testResults = [ | |
testFunctionWorks(findLength, list, length), | |
testFunctionWorks(accessLastItem, list, lastItem), | |
]; | |
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