Skip to content

Instantly share code, notes, and snippets.

@liseferguson
Created March 13, 2018 01:44
Show Gist options
  • Save liseferguson/a33d57ee4e5b0de3a7b0835a8e4dad55 to your computer and use it in GitHub Desktop.
Save liseferguson/a33d57ee4e5b0de3a7b0835a8e4dad55 to your computer and use it in GitHub Desktop.
index.js
function findLength(array) {
return array.length;
}
function accessLastItem(array) {
return array[array.length - 1];
}
/* In the 2nd function, why is the array before the [array.length - 1] necessary?
/* 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();
@liseferguson
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment