Skip to content

Instantly share code, notes, and snippets.

// To complete this drill, you need to write 2 functions, firstFourItems and lastThreeItems. Both functions should take a single argument: an array of values called array. You can assume that array includes at least 2 values, but may include more.
// firstFourItems should return a new array that copies the first 4 items in the array.
// lastThreeItems should return a new array that copies the last 3 items in the array.
function firstFourItems(array) {
return array.slice(0,4);
// To 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.
function findLength(array) {
return array.length;
}
function accessLastItem(array) {
return array[(array.length - 1)];
}
// 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) {
return array[0];
}
function accessThirdItem(array) {
return array[2];
}
/ 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 later in the module.
function addToList(list, item) {
list.push(item);
return list;
// your code goes here
}
/* From here down, you are not expected to
understand.... for now :)
// 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) {
return [item1, item2, item3];
}
/* From here down, you are not expected to
understand.... for now :)
function main() {
try {
doAllTheThings();
} catch (error) {
reportError(error);
}
}
function doAllTheThings() {
throw {
function doTrafficLights () {
const activeLight = getActiveLight();
if (activeLight === 'red') {
turnRed();
}
else if (activeLight ==='yellow') {
turnYellow();
}
else {
function isDivisible(dividend, divisor) {
let remainder = dividend % divisor;
return (0 == remainder);
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!
function celsToFahr(celsTemp) {
return celsTemp * 9/5 + 32;
}
function fahrToCels(fahrTemp) {
return (fahrTemp - 32 ) * 5/9;
}
/* From here down, you are not expected to
understand.... for now :)
function computeArea(width, height) {
return width * height;
}
/* From here down, you are not expected to
understand.... for now :)
Nothing to see here!