Skip to content

Instantly share code, notes, and snippets.

@AppMkrATL
Created February 19, 2018 15:09
Show Gist options
  • Save AppMkrATL/a60b7c396f60f371057676451e11f399 to your computer and use it in GitHub Desktop.
Save AppMkrATL/a60b7c396f60f371057676451e11f399 to your computer and use it in GitHub Desktop.
// Creating arrays
// ===============
/* Write a function called makeList that takes 3 arguments (item1, item2, item3) and returns an array containing the 3 items. */
function makeList(item1, item2, item3) {
// your code here
return [item1, item2, item3];
}
console.log(makeList("cat","dog","rabbit"));
/* 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();
//Adding array items
//==================
/* Write a function called addToList that accepts a list and an item as arguments, and returns the array with the item added to the end. This function will have the side effect of changing the original array. Usually, that's something to avoid, but it's fine here since we just want you to practice working with arrays. You'll learn more about side effects in the next lesson.
*/
function addToList(list, item) {
// your code goes here
list.push(item);
return list
}
console.log(addToList(["cat","dog","rabbit"],"bird"))
/* 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();
// Accessing array items
//======================
/*To complete this drill, you need to implement 2 functions, accessFirstItem and accessThirdItem. Both functions should take a single argument: an array of values called array. accessFirstItem should return the first item in the array, and accessThirdItem should return the third item in the array.
*/
function accessFirstItem(array) {
// your code goes here
return array[0]
}
console.log(accessFirstItem(["cat","dog","rabbit"],"bird"))
function accessThirdItem(array) {
// your code goes here
return array[2]
}
console.log(accessThirdItem(["cat","dog","rabbit"],"bird"))
/* 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();
// Array length and access
// =======================
/*o complete this drill, you need to create 2 functions, findLength and accessLastItem. Both functions should take a single argument: an array of values called array. findLength should return the length of array (i.e., the number of values in the array). accessLastItem should return the last value in the array without altering array itself.
*/
var array = ["cat","dog","rabbit","bird"];
function findLength(array) {
// your code goes here
return array.length
}
console.log(array.length)
function accessLastItem(array) {
// your code goes here
return array[array.length-1]
}
console.log(array[array.length-1])
console.log(array[3])
/* 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