Module 1: Array Methods 1, 2,3,4,5 Flashcards
Write a function called “getNthElement”.
Given an array and an integer, “getNthElement” returns the element at the given integer, within the given array.
function getNthElement(array, n) {
return array[n];
}
Write a function called “getFirstElement”.
Given an array, “getFirstElement” returns the first element of the given array.
Notes:
function getFirstElement(array) {
return array[0];
}
Write a function called “getLastElement”.
Given an array, “getLastElement” returns the last element of the given array.
function getLastElement(array) {
return array[array.length-1];
}
Write a function called “addToFront”.
Given an array and an element, “addToFront” adds the given element to the front of the given array, and returns the given array.
function addToFront(arr, element) {
arr.unshift(element);
return arr;
}
Write a function called “addToBack”.
Given an array and an element, “addToBack” returns the given array with the given element added to the end.
function addToBack(arr, element) {
arr.push(element);
return arr;
}
Write a function called “computeAreaOfARectangle”.
Given the length and width of a rectangle, “computeAreaOfARectangle” returns its area.
function computeAreaOfARectangle(length, width) {
return length * width;
}
Write a function called “computePerimeterOfARectangle”.
Given a length and a width describing a rectangle, “computePerimeterOfARectangle” returns its perimter.
function computePerimeterOfARectangle(length, width) { return (length \* 2) + (width \* 2); }
Write a function called “computePerimeterOfATriangle”.
Given 3 sides describing a triangle, “computePerimeterOfATriangle” returns its perimter.
function computePerimeterOfATriangle(side1, side2, side3) { return side1 + side2 + side3; }
Write a function called “computeTripledAreaOfARectangle”.
Given a length and width of a rectangle, “computeTripledAreaOfARectangle” returns the rectangle’s area, multiplied by 3.
function computeTripledAreaOfARectangle(length, width) {
return (length * width) * 3;
}
Write a function called “computePerimeterOfACircle”.
Given the radius of a circle, “computePerimeterOfACircle” returns its perimeter.
Notes:
Math.PI can be used for pi.
function computePerimeterOfACircle(radius) { return 2 \* Math.PI \* radius; }
Write a function called “computeAreaOfACircle”.
Given the radius of a circle, “computeAreaOfACircle” returns its area.
function computeAreaOfACircle(radius) {
return Math.PI * radius * radius;
}
Write a function called “computePower”.
Given a number and an exponent, “computePower” returns the given number, raised to the given exponent.
function computePower(num, exponent) {
return num ** exponent;
}
Write a function called “computeSquareRoot”. Given a number, “computeSquareRoot” returns its square root.
function computeSquareRoot(num) {
return Math.sqrt(num);
}
Write a function called “doubleSquareRootOf”. Given a number, “doubleSquareRootOf” returns double its square root.
function doubleSquareRootOf(num) {
return (Math.sqrt(num)) * 2;
}
Write a function called “getLengthOfThreeWords”.
Given 3 words, “getLengthOfThreeWords” returns the sum of their lengths.
function getLengthOfThreeWords(word1, word2, word3) {
return word1.length + word2.length + word3.length;
}
Write a function called “joinArrays”.
Given two arrays, “joinArrays” returns an array with the elements of “arr1” in order, followed by the elements in “arr2”.
function joinArrays(arr1, arr2) {
return arr1.concat(arr2);
}
Write a function called “getElementsAfter”.
Given an array and an index, “getElementsAfter” returns a new array with all the elements after (but not including) the given index.
function getElementsAfter(array, n) {
return array.slice(n + 1);
}
Write a function called “getElementsUpTo”.
Given an array and a index, “getElementsUpTo”, returns an array with all the elements up until, but not including, the element at the given index.
function getElementsUpTo(array, n) { return array.slice(0,n); }
Write a function called “getAllElementsButFirst”.
Given an array, “getAllElementsButFirst” returns an array with all the elements but the first.
function getAllElementsButFirst(array) {
return array.slice(1, array.length);
}
Write a function called “getAllElementsButLast”.
Given an array, “getAllElementsButLast” returns an array with all the elements but the last.
function getAllElementsButLast(array) {
return array.slice(0, array.length - 1);
}
Write a function called “removeFromFront”.
Given an array, “removeFromFront” returns the given array with its first element removed.
function removeFromFront(arr) {
arr.shift();
return arr;
}
Write a function called “removeFromBackOfNew”.
Given an array, “removeFromBackOfNew” returns a new array containing all but the last element of the given array.
function removeFromBackOfNew(arr) { return arr.slice(0, arr.length-1); }
Write a function called “removeFromFrontOfNew”.
Given an array, “removeFromFrontOfNew” returns a new array containing all but the first element of the given array.
function removeFromFrontOfNew(arr) {
return arr.slice(1, arr.length);
}
Write a function called “countCharacter”.
Given a string input and a character, “countCharacter” returns the number of occurences of a given character in the given string.
**function countCharacter(str, char) { var count = 0; for (var i = 0; i \< str.length; i++) { if (str[i] === char) { count++; } } return count; }**