Array methods Flashcards
1
Q
findIndex(array, value)
A
function findIndex(array, value) { for (var i = 0; i < array.length; i++) { var checkVal = array[i]; if (checkVal === value) { return i; } } return -1; }
2
Q
getIndexes(array)
A
function getIndexes(array) { var index = []; for (var i = 0; i < array.length; i++) { index.push(i); } return index;
}
3
Q
function countdown(number) {
A
function countdown(number) { var arr = []; for (var i = number; i >= 1; i--) { arr.push(i);
}
return arr;
}
4
Q
Get the tail end of an array
A
function tail(array) { var newArr = []; for (var i = 1; i < array.length; i++) { var index = array[i]; console.log(index); newArr.push(index); } return newArr;
}
5
Q
How to distinguish i and array [i]
A
{name: ‘John’}
Array is an object prototype, so if you’re looking for the property value, like object[key] it will give you ‘John’;
in an array array[i] gives you the value, where as i gives you the property, or index
6
Q
How to omit all falsy values in an array?
A
if(array[i])
that means if truthy