Array Methods Flashcards

1
Q

.concat()

  1. What is it’s purpose?
  2. What is the syntax?
  3. What does it return?
A
  1. used to merge two or more arrays
  2. const newArr = arr1.concat(arr2, arr3)
  3. a new array instance
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

.every()

  1. What is it’s purpose?
  2. What is the syntax?
  3. What does it return?
A
  1. tests whether all the elements in the array pass the test implemented by the provided function
  2. array.every( (el) => el < 40 )
  3. true/false

**calling .every() on an empty array will return true for every conditional

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q
  1. What method is used to merge to arrays ?

2. What is the syntax for using it?

A

.concat()

const newArr = arr1.concat(arr2, arr3)

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

.fill()

  1. What is it’s purpose?
  2. What is the syntax?
  3. What does it return?
A
  1. changes all elements in an array to a static value, from a start index (default 0) to an end index (default array.length)
  2. myArr = [1, 2, 3, 4]

(1 arg- value all indexes)
myArr.fill(6) // [6, 6, 6, 6]

(2 arg- value, starting index)
myArr.fill(5, 1) //[1, 5, 5, 5]

(3 arg- value, start, end)
myArr.fill(0, 2, 4) //[1, 2, 0, 0]

  1. returns the modified array
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

const myArr = [5, 7, 9, 11, 3]

What does this return?

  1. myArr.fill(6)
  2. myArr.fill(2, 1, 3)
  3. myArr.fill(4, 2)
A
  1. [6, 6, 6, 6, 6]
  2. [5, 2, 2, 2, 11]
  3. [5, 7, 4, 4, 4]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

.filter()

  1. What is it’s purpose?
  2. What is the syntax?
  3. What does it return?
A
  1. filtering an array containing elements that pass the test it implements
  2. words. filter(word=>word.length < 6)

array. filter(callbackfunction)
3. a new array

calls a provided callback function once for each element in an array, and constructs a new array of all the values for which callback returns a value that coerces to true. callback is invoked only for indexes of the array which have assigned values; it is not invoked for indexes which have been deleted or which have never been assigned values. Array elements which do not pass the callback test are skipped, and are not included in the new array

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

What does this return?

const array = [-3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13];

function tres (num){
    for(let i=3; num >=i; i++ {
        if (num % i !== 0) {
            return false;
        }
    }
    return num > 0;
}

array.filter(tres);

A

Multiples of 3

[3, 6, 9, 12]

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