Created
August 26, 2018 07:38
-
-
Save ecarlson1201/2f5c6e3d5e7b7b843d21c8e4c831e491 to your computer and use it in GitHub Desktop.
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) { | |
const myArray = array | |
return myArray[0] | |
} | |
function accessThirdItem(array) { | |
const myArray = array | |
return myArray[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) { | |
const myArray = list | |
myArray.push(item) | |
return myArray | |
} | |
/* 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) { | |
const myArray = [item1, item2, item3] | |
return myArray | |
} | |
/* 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) { | |
const myArray = array | |
return myArray.length | |
} | |
function accessLastItem(array) { | |
const myArray = array | |
return myArray.pop() | |
} | |
/* 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