Code Wars Flashcards

1
Q

What is the typeof operator?

A

The typeof operator returns a string indicating the type of the operand’s value. e.g. typeof_variable === ‘string’

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

What does Array.reduce() do and what is a simple or common example?

A

The reduce() method executes a user-supplied “reducer” callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

Array.reduce((a, b) => a + b, 0) This will add b to a (the previous total or accumulator) and it will start a 0.

more advanced example:
function countPositiesSumNegatives(input) {
if (!Array.isArray(input) || !input.length) return []
return input.reduce(arr, n) => {
if (n > 0) arr[0]++
if (n< 0) arr[1] += n
return arr
}, [0, 0])

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

What does String.parseInt() do?

A

The parseInt() function parses a string argument and returns an integer, AKA converts a string to a number (type of NaN is number)

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

What does String.repeat() do?

A

The repeat() method constructs and returns a new string which contains the specified number of copies of the string on which it was called, concatenated together.

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

What is Array.filter()? Give a basic example.

A

The filter() method creates a shallow copy (assignment to a variable is not needed) of a portion of a given array, filtered down to just the elements from the given array that pass the test implemented by the provided function.

const three = [1, 2, 3]
three.filter(num => num === 1)
// [1]

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

What is Array.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.

^^^First index^^^

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

What about Array.sort() ?

A

The sort() method sorts the elements of an array in place and returns the reference to the same array, now sorted.

*An in-place algorithm updates its input sequence only through replacement or swapping of elements.

array.sort((a, b) => a - b) ascending (b - a) descending

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

What is Array.map() ?

A

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

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