Arrays Flashcards

1
Q

What is an array?

A

Array ares a data structure.

Way store and organize data

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

Why are arrays better than variables?

A

Variables store strings, numbers, and booleans.

Variables can store single value only– which is limiting.

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

What types of data can arrays hold?

A

Strings

Numbers

Booleans

Even Other Arrays

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

What are 2 practical examples where arrays can be used?

A

Shopping list

Online order list

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

What’s the syntax of an array?

A

variable name = [];

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

What symbol do you use to separate list values?

A

commas

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

Does the last value in the list of array items have a comma?

A

No

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

Why should you breakup items?

A

Easier to read

Easier to edit

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

What is zero index?

A

The counting starts at 0.

Rather than at 1

First item is at 0 index.

Second item is at 1 index.

Third item is at 2 index.

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

What symbol do you use to return values?

A

[]

brackets

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

What happens if you list an item not on the list?

Example [4]

A

Last item is undefined in console because nothing in index 4.

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

script.js contains an array of string values (the names of players in a game). Practice using array indices by accessing individual names in the players array. Start by logging the first name inside the array using the console.log() method.

First Response:

const players = [‘Toni’, ‘Anwar’, ‘Mali’, ‘Carlos’, ‘Cormac’, ‘Sariah’];

A

Notice the syntax:

Parenthesis

variable name

Brackets

;

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

Complete the code below to log the message “Arrays are awesome” to the console:

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

Arrays are zero-based. What does that mean?

A

The index of the first element inside an array is 0 (not 1).

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

Which of the following best describes an array?

A

A collection of values you can assign to a single variable.

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

In this array, which code snippet prints the number 1 to the JS console?

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

What’s a great resource for JS?

A

MDN

Mozilla Developer Network

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

What does the length property do?

A

returns values in array

number of items in array

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

How do you add elements?

A

Use .push to add to the end of the array.

Use .unshift to the beginning of the array

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

What does .push do?

A

Adds elements

Adds to the end of an array

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

What does .unshift do?

A

.unshift adds an element to the beginning of an array

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

The array assigned to the guestList variable holds a list of names. Add two new names to the end of the array using the push() method. Make sure not to type the names directly inside the guestList array 😁.

A

variableName.push(‘Name1’, ‘Name2’);

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

Now add two names to the beginning of the array using the .unshift array method

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

How do you remove elements from an array?

A

.pop and .shift methods remove items from an array

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
How does the .pop method remove items?
.pop takes off the last item from the array
26
How does the .shift method remove elements?
Takes first item off from an array
27
How do you remove tuna from the list?
let [new variable name] = [variableWithList].shift();
28
What methods help create first in, first out?
.Push and .shift method work together:
29
What happens if you write numbers.length given this array?
It returns the length of 5
30
What happens if you apply numbers.push(600)?
Adds 600 to the const numbers array .push adds to the end of an array
31
What happens if you apply numbers.unshift(0)?
You add 0 to beginning of an array
32
What happens if you apply numbers.shift()
Returns 0
33
The array assigned to the variable orderQueue contains a list of order numbers. Declare a new variable named shipping. Remove the first element from the orderQueue array and assign it to the shipping. variable.
34
Next, declare a new variable named cancelled. Remove the last item from the orderQueue array and assign it to the variable cancelled. Use the array that method removes the last element from an array and returns it.
.pop() returns last value removes last value
35
What is the spread operator look like?
3 dots + variable name
36
What does the spread operator do?
The spread operator (...) a JS syntax that builds, combines, and manipulate arrays quickly and seamlessly.
37
How do you connect HTML and JS file?
Add a JS script tag to the index.html
38
What is a two-dimensional array?
It's an array within an array.
39
How do you connect arrays with the spread operator?
You use the 3 dots and variable name Example ...middle
40
How do you bring together two arrays into a new variable altogether?
Use spread operators ... with variable names
41
How would you remove an array?
Use woodwind.pop() It would only remove from woodwind array
42
How do you add items to the array to the front?
variableNames.unshift('item', 'itemB')
43
How would you extract the max number in a list of numbers using the spread operator? const numbers = [10, 20, 30, 40];
See the console.log
44
What does Math.max do?
Produces the highest number in a list within an array
45
What does Math.min do?
It selects the lowest number within an array.
46
Can you use the .push() method to add more than one element to an array at the same time? Is the following example, valid? scores.push( 77, 76, 89);
Yes. You can use push() to add any number of elements to the end of an array.
47
Please fill in the correct answer. Complete the code by using the property that returns the number of elements in the attendees array.
attendees.length
48
Which array method adds an element to the end of an array?
.push()
49
Which of the following places the contents of one array into another array?
The spread operator (...)
50
Which array method returns the first element of an array and removes it from the array?
**.shift()** removes first item from beginning of the array
51
Complete the code below to add the value of 23 the beginning of the ages array.
ages.unshift(23);
52
What is the zero index values for the following array of students?
Order/Name 0 Lee 1 Jan 2 Mali 3 Sarah
53
What happens when the loop counter reaches 4?
When it reaches 4, it breaks the loop as the array doesn’t have a 4th item.
54
Why would you use a variable length counter?
students.length is superior coding because it adjusts to changes in the array better If student number changes, up/down, program is flexible enough to run.
55
What function would you use to create items in a list?
function is createListItems
56
What is arr?
Parameter
57
What does the ${arr[i]}?
It returns values that matches i variable
58
What's the role of return items;?
it’s key to calling function
59
What does the document.querySelector section of code produce?
It puts all the songs in an ordered playlist
60
What symbol do you use to keep conditions separate?
use the semi-colon ; to separate conditions
61
Do you need a symbol after the closing curly brace }?
No, you don't need a ; after a } curly brace.
62
What does the the .join array method do?
.join() method creates and returns a new string by concatenating all of the elements in an array, separated by commas or specified separator string. Joins list items in a variable by giving it a format consisting with spaces, dashes, or punctuation
63
What does the array method .includes() do?
determines whether or not an item is found within an array by providing an answer as True or False for Yes and No respectively true or false true: found false: not found
64
If an item is found within an array, what response does the .includes() array method provide?
True
65
What happens if you use the array method .includes() for an item that's not found within an array, what response is provided in the JavaScript console?
False Not found in the array
66
Is the .includes() array method case sensitive?
Yes It's case sensitive. The wrong case creates an error Stick to lowercase
67
What does **.indexOf** array method do?
Checks if value is found within an array
68
Using the .indexOf array method how does it use the zero index method?
-1 not found if it is found in the array
69
What does a positive number mean in the context of the .indexOf array method mean?
Positive numbers means the item is found in the list of array items
70
What does a negative number mean in the context of .indexOf()?
Negative number = not in the list of array items
71
Use the array method that combines all of the elements inside the planets array into a single string. The final string should separate the array elements by a comma and space. Log the final string value to the console.
Logs the final string value to the console with the .join array method giving it a comma and space to bring the list items together with correct formatting
72
Next, use the method that returns the position of 'Saturn' in the planets array. Log the return value to the console.
console.log(variableName.indexOf('Saturn'));
73
How do you write an if/else statement to search an array based on a search? Just the if/else section
if (variableName.includes(search) { message= `Yes, we have ${search}`; } else { message = `Sorry, we don't have ${search}`;
74
How do you explain the logic behind this program?
FIrst, establish a variable assigned to an array with the list of items. Second, create a variable that asks a prompt question Create a third variable for the reply to the question Fourth, create an if/else statement that searches the first variable using the second variable question to then produce yes/no messages if item is or isnt in stock
75
What array method would you use to search the grocery list items to see if it is in stock?
.indexOf array method
76