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;
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

getIndexes(array)

A
function getIndexes(array) {
  var index = [];
  for (var i = 0; i < array.length; i++) {
    index.push(i);
  }
  return index;

}

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

function countdown(number) {

A
function countdown(number) {
  var arr = [];
  for (var i = number; i >= 1; i--) {
    arr.push(i);

}
return arr;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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;

}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
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

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

How to omit all falsy values in an array?

A

if(array[i])

that means if truthy

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