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;
}