Skip to content

Instantly share code, notes, and snippets.

@ukhlivanov
Created April 29, 2018 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukhlivanov/a6d63f2e04cfd3904cc03f851c46c5c8 to your computer and use it in GitHub Desktop.
Save ukhlivanov/a6d63f2e04cfd3904cc03f851c46c5c8 to your computer and use it in GitHub Desktop.
ThinkFul-Arrays
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();
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();
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();
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