Method Flashcards

1
Q

Define .slice( ) method.

A

The slice( ) method returns a shallow copy of a portion of an array into a new array object selected from start to end. (start and end represent the index of items in that array. end is not included)
ex.
const numbers=[1,2,3,4,5]
numbers.slice(1,3) = [2,3]
numbers.slice()=[1,2,3,4,5] => returns shallow copy

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

Define .indexOf( )

A

The indexOf( ) method returns the first index at which a given element can be found in the array. or -1 if it is not present.
ex.
‘abc’.indexOf(‘b’) = 1
‘abc’.indexOf(‘x’) =-1

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

.push( )

A

the push( ) method adds one or more elements to the end of an array and returns the new length of the array

ex.
const animals =[‘pigs’, ‘goats’, ‘sheep’]
const count = animals.push(‘cows’)
console.log(count); 4
console.log(animals); [[‘pigs’, ‘goats’, ‘sheep’, ‘cows’]

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

.pop( )

A
The pop( ) method removes the last element from an array and returns that element.
ex.
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato'];
console.log(plants.pop()); // expected output: "tomato"

console.log(plants); // expected output: Array [“broccoli”, “cauliflower”, “cabbage”, “kale”]

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

.shift( )

A

The shift( ) method removes the first element from an array and returns that removed element.

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

arr.unshift( )

A

The unshift( ) method adds one ore more elements to the beginning of an array and returns the new length of the array
ex. arr. unshift(element1[,…[,elementN]])
const array = [1,2,3]
array.unshift(4,5)
console.log(array)// [1,2,3,4,5]

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

arr. join(‘-‘) // ‘x-b-c’
arr. join(‘ ‘) // ‘xbc’
arr. join( ) // ‘x,b,c’

A

all elements in a single string

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

.map( )

A

It takes an argument of a callback funtion and returns a new array.
it works in a similar manner to .forEach()–the major difference is that .map returns a new array.

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

.forEach( )

A

iteration method.
it executes the same code for each element of an array.
loops through the array and executes the callback function for each element. During each execution, the current element is passed as an argument to the callback function.
the return value for .forEach() will always be undefined.

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

.filter( )

A

Returns a new array. However, it returns an array of elements after filtering out certain elements from original array. callback function for the .filter( ) method should return true or false.
The elements that causes the callback function to return true are added to the new array.

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

callback function

A

a function passed as an argument into another function

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

.findIndex( )

A

it returns the index of the first element that evaluates to true in the callback function. it returns -1 if none of the elements in the array satisfies the condition

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

? true : false;

A

return true or else return false

? -> ternary operator
: -> colon separator

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

.reduce( )

A

It returns a single value after iterating through the elements of an array, thereby reducing the array.

value of acculumator= starts off as the value of the first element in the array. The return value of the callback function becomes the accumulator
value of currentValue = starts as the second element.it takes on the value of the current element in the looping process.

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

.some( )

A

tests whether at least one element in the array passes the test implemented by the provided function

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

.every( )

A

tests whether all elements in the array pass the test implemented by the provided function. it reutrns boolean value.

17
Q

toString()

A

returns a string