Skip to content

Instantly share code, notes, and snippets.

@ecarlson1201
Created August 26, 2018 07:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecarlson1201/2f5c6e3d5e7b7b843d21c8e4c831e491 to your computer and use it in GitHub Desktop.
Save ecarlson1201/2f5c6e3d5e7b7b843d21c8e4c831e491 to your computer and use it in GitHub Desktop.
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();
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();
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();
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