Arrays Flashcards

1
Q

What is an array?

A
  • It is a data structure to store ordered collections
  • Array elements are numbered starting with zero
  • Arrays have many methods to manage the order of elements
  • Can be created by a constructor or literal notation
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you create an array?

A

you can create a list of movies as follows.

const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];

Everything in the bracket makes up the array

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

Can an array contain different types of values like string, number, booleans, objects?

A

Yes

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

How do you access the array size?

A

The size of an array is the number of object in it and can be accessed as follows:

const arrayName = [ ]; // Create an empty array
console.log(arrayName.length); // 0
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How to access a particular element in an array?

A

You can access a particular element passing its index in squared braket

const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];

console. log(movies[0]); // “The Wolf of Wall Street”
console. log(movies[1]); // “Zootopia”
console. log(movies[2]); // “Babysitting”

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

How do you update arrays content?

A

You add a new item to an array with the “push()” method. The new element to be added is passed as a parameter to the method. It is inserted at the end of the array.

const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];

movies. push(“Ghostbusters”);
console. log(movies[3]); // “Ghostbusters”

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

How do you remove an element from an arrays?

A

You can use “pop()” or you can use “splice()”:

“pop()” : let you remove the last element

“splice()” : it need two parameters the first one is the index from which to begin removing, and the second one is the number of elements to remove.
E.g
const movies = [“The Wolf of Wall Street”, “Zootopia”, “Babysitting”];
movies.splice(0, 1);

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

Main methods to add or remove items from arrays?

A
  • arr.push(…items) – adds items to the end,
  • arr.pop() – extracts an item from the end,
  • arr.shift() – extracts an item from the beginning,
  • arr.unshift(…items) – adds items to the beginning.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the syntax for creating an array with

  • Literal Notation
  • Array constructor
A
Literal notation: let newArr = []
Array constructor: let newArr = Array()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are arrays populated with?

A

Elements

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

newArr = [“Zebra” , , ture,21]

What is the element at index number 1 and 4

A

Undefined, because it is an empty space and nothing because the array stops at index 3

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

How to get the last element from an array?

A

let lastElement = nameOfArray [nameOfArray - 1 ]

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

Arrays iteration’

How do you iterate through an array passing the value and index for each elements?

A

Taking this example of array

let bestColors = [‘green’, ‘blue’, ‘yellow’, ‘black’]
bestColors.forEach((x,i)=> console.log(x,i,a))

x: will display the elements
i: will display the index
a: will display the whole array

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

What is the method “.forEach” used for?

A

.forEach is a built in function that allows to to run a function for every element of the array.

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

What is the syntax of the .forEach method?

A

arrayName.forEach(function(item, index, array) {
do something with item
});

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

How can I assign a whole array to a different one?

A

let arrayOne = [ … ]

letArrayTwo = [ … ]

arrayOne = arrayTwo

17
Q

What methods are used to transform an array?

A
  • map(func) – creates a new array from results of calling func for every element.
  • sort(func) – sorts the array in-place, then returns it.
  • reverse() – reverses the array in-place, then returns it.
  • split/join – convert a string to array and back.
  • reduce/reduceRight(func, initial) – calculate a single value over the array by calling func for each element and passing an intermediate result between the calls.
18
Q

What does the method ‘Filter’ do to an array?

arr.filter

A

‘Filter’ create an new array from an existing array and it run as many times as the elements in the array and if the element satisfies the function they are added to the new array

19
Q

How do you sort an array of numbers?

A
function functionName(arr){
    let sorted = arr.sort((a,b) => a-b)
}
20
Q

What can you use to move through arrays to use the content in them? (or iterates)

A
  • For loop

- For.each loop

21
Q

how to sum all the number in an array?

A

Array.reduce( (acc,c) => acc + c, 0 )

22
Q

What does the method “.reduce” does?

A

It sets up a bucket for the accumulation value and it “reduce” down to a singular number.

23
Q

How does the method “.reduce” works?

A

This is going to run once for every in the array and it is going to grab 2 important value accumulator and the current value. Then it will add the current value to the accumulator. At the end of the function it will be then take a value that indicate the starting point.

24
Q

What is the syntax of “.reduce” method?

A

Eg.

Array.reduce( (acc,c) => acc + c, 0 )

25
Q

Array.reduce( (acc,c) => acc + c, 0 )

What the c stand for in this reduce method?

A

Current value

26
Q

What does the method ‘.map’ do?

A

Creates a new array populated with the results of calling a provided function on every element in the calling array

27
Q

How do you print the reverse of a given string?

A

let reverse = str => console.log( str.split(‘…’).reverse().join(‘…’) )

28
Q

what are the 2 ways for creating an array?

A
  • Literal notation

- constructor

29
Q

What is an integer array?

A

It is an array of numbers

30
Q

How can the ‘reduce’ method be explained in simple terms?

A

reduce takes the value in one array and reduce them to one number. The ‘acc’ and ‘c’ value are defined in parenthesis. Then we add the current value from the array ‘c’ to acc. (‘c’ can also be squared cube divide by… etc ) at the end we insert comma 0 to establish where we start from.