Module 1: Array Methods 1, 2,3,4,5 Flashcards

1
Q

Write a function called “getNthElement”.

Given an array and an integer, “getNthElement” returns the element at the given integer, within the given array.

A

function getNthElement(array, n) {
return array[n];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Write a function called “getFirstElement”.

Given an array, “getFirstElement” returns the first element of the given array.

Notes:

A

function getFirstElement(array) {
return array[0];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Write a function called “getLastElement”.

Given an array, “getLastElement” returns the last element of the given array.

A

function getLastElement(array) {
return array[array.length-1];
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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.

A

function addToFront(arr, element) {
arr.unshift(element);
return arr;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Write a function called “addToBack”.

Given an array and an element, “addToBack” returns the given array with the given element added to the end.

A

function addToBack(arr, element) {
arr.push(element);
return arr;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Write a function called “computeAreaOfARectangle”.

Given the length and width of a rectangle, “computeAreaOfARectangle” returns its area.

A

function computeAreaOfARectangle(length, width) {
return length * width;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Write a function called “computePerimeterOfARectangle”.

Given a length and a width describing a rectangle, “computePerimeterOfARectangle” returns its perimter.

A
function computePerimeterOfARectangle(length, width) {
return (length \* 2) + (width \* 2);
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Write a function called “computePerimeterOfATriangle”.

Given 3 sides describing a triangle, “computePerimeterOfATriangle” returns its perimter.

A
function computePerimeterOfATriangle(side1, side2, side3) {
return side1 + side2 + side3;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Write a function called “computeTripledAreaOfARectangle”.

Given a length and width of a rectangle, “computeTripledAreaOfARectangle” returns the rectangle’s area, multiplied by 3.

A

function computeTripledAreaOfARectangle(length, width) {
return (length * width) * 3;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Write a function called “computePerimeterOfACircle”.

Given the radius of a circle, “computePerimeterOfACircle” returns its perimeter.

Notes:

Math.PI can be used for pi.

A
function computePerimeterOfACircle(radius) {
return 2 \* Math.PI \* radius;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Write a function called “computeAreaOfACircle”.

Given the radius of a circle, “computeAreaOfACircle” returns its area.

A

function computeAreaOfACircle(radius) {
return Math.PI * radius * radius;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Write a function called “computePower”.

Given a number and an exponent, “computePower” returns the given number, raised to the given exponent.

A

function computePower(num, exponent) {
return num ** exponent;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write a function called “computeSquareRoot”. Given a number, “computeSquareRoot” returns its square root.

A

function computeSquareRoot(num) {
return Math.sqrt(num);
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Write a function called “doubleSquareRootOf”. Given a number, “doubleSquareRootOf” returns double its square root.

A

function doubleSquareRootOf(num) {
return (Math.sqrt(num)) * 2;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Write a function called “getLengthOfThreeWords”.

Given 3 words, “getLengthOfThreeWords” returns the sum of their lengths.

A

function getLengthOfThreeWords(word1, word2, word3) {
return word1.length + word2.length + word3.length;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

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”.

A

function joinArrays(arr1, arr2) {
return arr1.concat(arr2);
}

17
Q

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.

A

function getElementsAfter(array, n) {
return array.slice(n + 1);
}

18
Q

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.

A
function getElementsUpTo(array, n) {
return array.slice(0,n);
}
19
Q

Write a function called “getAllElementsButFirst”.

Given an array, “getAllElementsButFirst” returns an array with all the elements but the first.

A

function getAllElementsButFirst(array) {
return array.slice(1, array.length);
}

20
Q

Write a function called “getAllElementsButLast”.

Given an array, “getAllElementsButLast” returns an array with all the elements but the last.

A

function getAllElementsButLast(array) {
return array.slice(0, array.length - 1);
}

21
Q

Write a function called “removeFromFront”.

Given an array, “removeFromFront” returns the given array with its first element removed.

A

function removeFromFront(arr) {
arr.shift();
return arr;
}

22
Q

Write a function called “removeFromBackOfNew”.

Given an array, “removeFromBackOfNew” returns a new array containing all but the last element of the given array.

A
function removeFromBackOfNew(arr) {
return arr.slice(0, arr.length-1);
}
23
Q

Write a function called “removeFromFrontOfNew”.

Given an array, “removeFromFrontOfNew” returns a new array containing all but the first element of the given array.

A

function removeFromFrontOfNew(arr) {
return arr.slice(1, arr.length);
}

24
Q

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.

A
**function countCharacter(str, char) {
var count = 0;
 for (var i = 0; i \< str.length; i++) {
 if (str[i] === char) {
 count++;
 }
}
 return count;
}**