Arrays in JavaScript Flashcards

1
Q

What are arrays?

A

A data type that deals with long lists of information.

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

How do you create an array?

A

Use two square brackets and separate the items in it with commas like so:

let myArray = [1, ‘two’];

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

How do you access an item in an array?

A

Ask for the item in a given position in the array. For example:

let myArray = [1, 2, 3];

myArray[0]; // => answer is 1

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

What is the first number in an array list?

A

0

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

How do you add an item to the end of an array list?

A

Use the built-in .push method like so:

let myArray = [1, 2, 3];
myArray.push(4);

console.log(myArray); // => [1, 2, 3, 4]

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

Can use you const for an array?

A

Yes, even though the values in it may change. You can use both let and const.

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

How do you determine how long an array is?

A

Use the .length method like so:

const myArray = [1, 2, 3, 4];
console.log(myArray.length); // => 4
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the .pop() array method?

A

.pop() is used to “pop” a value off of the end of an array. We can store this “popped off” value by assigning it to a variable. In other words, .pop() removes the last element from an array and returns that element. Any type of entry can be “popped” off of an array - numbers, strings, even nested arrays.

var threeArr = [1, 4, 6];
var oneDown = threeArr.pop();

console. log(oneDown); // Returns 6
console. log(threeArr); // Returns [1, 4]

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

What is the .shift() array method?

A

Like .pop(), but removes the first item of a list:

const myArray = [1, 2, ‘three’];

console.log(myArray.length); // Returns 3

const firstItem = myArray.shift();

console. log(firstItem); // Returns 1
console. log(myArray.length); // Returns 2

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

What is this array method? .slice(begin, end)

A

This method creates a new array from an existing one. If you call it with no arguments, the entirety of the original array will be copied. It can be called with begin and end to specify only part of the original array has been copied. Finally, you can use negative values for the begin parameter in order to select the last n numbers of an array.

const myArray = [1, 2, 3, 4];

myArray.slice(); // => [1, 2, 3, 4]

const copyArray = myArray.slice(0, 2)
console.log(copyArray); // => [1, 2]

myArray.slice(-2); // => [3, 4];

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

What is this array method? .sort()

A

This method sorts an array in place. In place means that this method sorts the original array; it does not copy the array, sort it, and then return the sorted copy. If you pass no arguments to .sort(), it will alphabetically sort the list by default:

const myArray = ['zebra', 'yodel', 'xylophone'];
myArray.sort();
console.log(myArray); // => ['xylophone', 'yodel', 'zebra']
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do you sort an array by criteria?

A

If you need to sort an array by some other criteria, you must pass in a compare function to .sort() that tells the computer the criteria for sorting any 2 items. This function must return a numeric value. If the returned value is negative, the second element comes before the first. If it’s postive, the first element comes before the second. And if it’s exactly 0, the 1 elements have the same numeric value.

The following example demonstrates how you can sort a list of numbers in ascending order.

const myArray = [200, 20, 2, 100, 1, 10];

console.log(myArray); // => [200, 20, 2, 100, 1, 10]

function sortNumbers(a, b) {
  return a - b;
}

myArray.sort(sortNumbers);
console.log(myArray) // => [1, 2, 10, 20, 100, 200]

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

What is this array method? .map()

A

This method is used to generate a new array of items by applying the same function to each item in the original array. Here we provide an example of using map to work with numbers, first using a named function, then with an anonymous (arrow) function:

function double(num) {
  return 2 * num;
}
const myNumbers = [1, 2, 3, 4];
const doubledNumbers = myNumbers.map(double);
console.log(doubledNumbers); // => [2, 4, 6, 8];
const doubledNumbersAlt = myNumbers.map(num => 2 * num);
console.log(doubledNumbersAlt); // => [2, 4, 6, 8];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is this array method? .forEach()

A

Like .map(), this method applies a function to each item in a collection, but it does not return an array of transformed elements.

const directionsLibrary = [‘wash’, ‘rinse’, ‘repeat’];

function saveDirectionInDatabase(direction) {
  // save the direction in the database
  console.log(direction);
}
directionsLibrary.forEach(saveDirectionInDatabase); // ->
// 'wash'
// 'rinse'
//'repeat'
// or
directionsLibrary.forEach(direction => console.log(direction)); // ->
// 'wash'
// 'rinse'
//'repeat'
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is this array method? .filter()

A

This method is used to take one array of items and make a new one that contains only items that a filtering function returns true for.

function isEven(num) {
  return num % 2 === 0;
}

const myNumbers = [1, 2, 3, 4, 5, 6];

const evens = myNumbers.filter(isEven);
console.log(evens); // => [2, 4, 6]
// or
const evensAlt = myNumbers.filter(num => num % 2 === 0);
console.log(evensAlt); // => [2, 4, 6];
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is this array method? .reduce()

A

This is useful for aggregating array data. This method iterates over the array while maintaining an accumulation object. This object is returned inside the reduce function and is passed into the subsequent iteration.

function sum(total, num) {
  return total + num;
};

const numbers = [1, 2, 3, 4];

console.log(numbers.reduce(sum)); // => 10

17
Q

What is this array method? .find()

A

This method is used to find a single item in an array. Here’s an example:

function isEven(num) {
  return num % 2 === 0;
}
const myNumbers = [1, 2, 3, 4, 5, 6];
myNumbers.find(isEven); // => 2
const myNumbers2 = [1, 3, 5, 7, 9];
myNumbers2.find(isEven); // => undefined
// or
myNumbers.find(num => num % 2 === 0); // => 2
myNumbers2.find(num => num % 2 === 0); // undefined
18
Q

How do you sort an array with an anonymous function?

A

const myArray = [200, 20, 2, 100, 1, 10];

myArray.sort(function(a, b) {
return a - b;
});

console.log(myArray) // => [1, 2, 10, 20, 100, 200]

You can also do it this way with an arrow function:

const myArray = [200, 20, 2, 100, 1, 10];
myArray.sort((a, b) => a - b);
console.log(myArray) // => [1, 2, 10, 20, 100, 200]

19
Q

What is a loop?

A

A loop is a construct that allows you to repeat a set of instructions a set number of times or until a specific condition is true.

20
Q

What are for loops?

A

Loops through a block of code a number of times. For example:

const countTo = 10;
for (let i = 1; i <= countTo; i++) {
  console.log(i);
}

This code will log the numbers 1 through 10 to the console. It says “Starting at i=1, log the value of i. After logging i, if i is less than or equal to the value of countTo, increment i by one, and then repeat the process.”

Note how the for keyword is followed by parentheses with three arguments: an initialized counter variable (let i = 1), a continue condition that if true means the loop will continue (i <= countTo), and a final expression that says how the counter is to be incremented each time through the loop (i++, which is basically a shortcut for i = i + 1.)

Use for loops whenever there is a set of operations that you want to do a set number of times. You’ll use them all the time in your daily life as a programmer.

21
Q

What are while loops?

A

Loops through a block of code while a specified condition is true. For example:

let counter = 1;
const countTo = 10;
while (counter <= countTo) {
   console.log(counter);
   counter++;
}

With while loops, you first have the while keyword, followed by a condition in parentheses. If the condition evaluates to true, the loop will run; if not, it won’t. This example is meant to demonstrate syntax, but in practice, a for loop is better suited for this particular use case.

Conceptually, while loops are meant to be used where the looped behavior does not have a known number of iterations but instead a known logical condition where it should terminate.

22
Q

How do you break a while loop?

A

Use a break command. For example:

while (true) {
  // do something
  if (someCondition) {
    break;
  }
}

Since the while condition here (true) will always be true, this loop will run forever unless (or until) the condition in the if block is true. If that is the case, we use the break command to break out of the while loop and move on to any remaining code.