Code Wars Flashcards
What is the typeof operator?
The typeof operator returns a string indicating the type of the operand’s value. e.g. typeof_variable === ‘string’
What does Array.reduce() do and what is a simple or common example?
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])
What does String.parseInt() do?
The parseInt() function parses a string argument and returns an integer, AKA converts a string to a number (type of NaN is number)
What does String.repeat() do?
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.
What is Array.filter()? Give a basic example.
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]
What is Array.indexOf() ?
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^^^
What about Array.sort() ?
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
What is Array.map() ?
The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.