JS Array Flashcards

1
Q

What is array in JS

A

Array is an ordered list of values /element whit index/

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

Array Characteristics

A

1.Array can hold values of different typs
2.Array is auto-growing, the length is dinamicaly sized

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

How create arr

A

let arr = newArray() //empty arr
let arr1 = newArray(10) //- array whit size 10
let arr2 = [el1, el2, el3]

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

Find array length
let arr3 = [‘el1’, ‘el2’, ‘el3’]

A

let arr3 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr3.length); //3

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

Add el in the end of arr
let arr4 = [‘el1’, ‘el2’, ‘el3’]

A

let arr4 = [‘el1’, ‘el2’, ‘el3’]
arr4.push(‘el4’)
console.log(arr4); //[ ‘el1’, ‘el2’, ‘el3’, ‘el4’ ]

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

add el in the begin of arr
let arr5 = [‘el1’, ‘el2’, ‘el3’]

A

let arr5 = [‘el1’, ‘el2’, ‘el3’]
arr5.unshift(‘el5’)
console.log(arr5); //[ ‘el5’, ‘el1’, ‘el2’, ‘el3’ ]

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

// removing el in the end of arr
let arr6 = [‘el1’, ‘el2’, ‘el3’]

A

let arr6 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr6.pop() //el3
console.log(arr6); //[ ‘el1’, ‘el2’ ]

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

removing el in the begin of arr
let arr7 = [‘el1’, ‘el2’, ‘el3’]

A

let arr7 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr7.shift() //el1
console.log(arr7); //[ ‘el2’, ‘el3’ ]

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

// Find index el of arr
let arr8 = [‘el1’, ‘el2’, ‘el3’]
el2

A

console.log(arr8.indexOf(‘el2’)) //1

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

Check is arr
let arr9 = [‘el1’, ‘el2’, ‘el3’]

A

console.log(Array.isArray(arr9)) //true

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

What is the Stack?

A

The data structure - list whit elements. Use LIFO principe Last in First out. Operations is only at the top of the stack: push() add one or more el in top of stack /end arr/ and pop() remove element of top of the stack /end arr/.

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

What is result?
let stack = [1,2,3]
stack.push(4)
console.log(stack);

A

push() add one or more el in top of stack /end/
console.log(stack); //[ 1, 2, 3, 4 ]

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

What is result?
let stack1 = [1,2,3]
console.log(stack1.pop())
console.log(stack1);

A

pop() remove element of top of the stack /end arr/
console.log(stack1.pop()) //3
console.log(stack1);//[ 1, 2 ]

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

What is result?
const reverse = (str) => {
let stack = [];
for (let i = 0; i < str.length; i++) {
stack.push(str[i]);
}

let reverseStr = “”;
while (stack.length > 0) {
reverseStr += stack.pop();
}
return reverseStr;
};
console.log(reverse(“Lia”));

A

reverse str with JS stack
console.log(reverse(“Lia”)); //aiL

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

What is the Queue?

A

The data structure - ordered list whit elements. Use FIFO principe First in First out. Operations is
enqueue insert el of the end of queue, /add el in the end of arr with push()/
dequeue removal from the front of queue.//remove el of the front of arr with shift()
peek -returns el et the front, no modify queue

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

Which method is used, what is the output?

let arr = [1, 2, 3, 4];
let delArr = arr.splice(0,3)
console.log(arr);
console.log(delArr);

A

use splice() for delete el from JS Arr two arguments 1. position to start 2. number of el for delete

console.log(arr); //[ 4 ]
console.log(delArr); //[ 1, 2, 3 ]

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

Which method is used, what is the output?

let arr1 = [ 1, 2, 3 ]
// insertArr
arr1.splice(3,0, 4,5,6,7)
console.log(arr1);

A

use splice() for insert one or more el to JS Arr
three arguments 1. position for insert 2.0 zero -not delete el
3. other arguments is el for insert
console.log(arr1); [
1, 2, 3, 4,
5, 6, 7
]

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

Which method is used, what is the output?
let arr2 = [ 1, 2, 3 ]
replaceArr
arr2.splice(1,1, 7)

console.log(arr2);

let arr3 = [ 1, 2, 3 ]
// replaceArr
arr3.splice(2,1, 8 ,11,12)

console.log(arr3);

A

use splice() for replace one or more el to JS Arr
three arguments 1. position for start 2.how much element for replace if 0 we only insert el
3. arguments is new el
\
console.log(arr2); //[ 1, 7, 3 ]

console.log(arr3); //[ 1, 2, 8, 11, 12 ]

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

Which method is used, what is the output?

let arr4 = [ 1, 2, 3 ]
let arr41 = arr4.slice()
console.log(arr41);
console.log(arr4);

A

// slice() return new arr have 2 parameters
// 1. start 2.stop
use slice for clone arr
console.log(arr41); //[ 1, 2, 3 ]
console.log(arr4); //[ 1, 2, 3 ]

20
Q

Which method is used, what is the output?
let arr5 = [ 1, 2, 3, 4, 5, 6, 7 ]
let arr51 = arr5.slice(1,3)
console.log(arr51);

A

use slice for copy part el of arr
console.log(arr51);[ 2, 3 ]

21
Q

Which method is used, what is the output?

let arr6 = [ 1, 2, 3, 4, 5, 6, 7 ]
console.log(arr6.indexOf(3))

A

IndexOf() to find index of el
let arr6 = [ 1, 2, 3, 4, 5, 6, 7 ]
console.log(arr6.indexOf(3));//2

22
Q

Which method is used, what is the output?

let arr7 = [1, 2, 6, 3, 4, 5, 6, 7];
const find = (el, arr) => {
let results = [];
let idx = arr.indexOf(el);
while (idx !== -1) {
results.push(idx);
idx = arr.indexOf(el, idx + 1);
}
return results;
};
console.log(find(6, arr7));

A

implement find with indexOf - every position of el -return arr with position
console.log(find(6, arr7)); [ 2, 6 ]

23
Q

Which method is used, what is the output?

let arr8 = [1, 2, 3, 4, 5, 6, 7];
let result = arr8.every(e => e > 0);
console.log(result);

A

use every to check el of arr
console.log(result); //true

24
Q

Which method is used, what is the output?

let arr9 = [1, 2, 3, 4];
let range = {
min: 0,
max: 10,
};

let isInRange = arr9.every(function (e) {
return e >= this.min && e <= this.max;
}, range);

console.log(isInRange);

A

check with every() is every el of arr is in range
console.log(isInRange); true

25
Q

Which method is used, what is the output?

let arrSome = [1, 2, 3, 4, 5, 6, 7];

const lessFive = arrSome.some((e) => e < 5);
console.log(lessFive);

A

// check with some() have el less then 5

//true

26
Q

Which method is used, what is the output?

let arrSome1 = [1, 2, 3, 4, 5, 6, 7];

const exist = (val, arr) => arr.some(e => e===val)
console.log(exist(4, arrSome1));

A

function exist with some()
true

27
Q

Which method is used, what is the output?
let arrSort = [11, 22, 53, 4, 5, 6, 7];
console.log(arrSort.sort((a,b) => a - b));

A

// To sort el in arr we use sort() for numbers and

console.log(arrSort.sort((a,b) => a - b)); //[4, 5, 6, 7, 11, 22, 53]

28
Q

Which method is used, what is the output?

let arrSort1 = [‘cat’, ‘been’, ‘ant’];
console.log(arrSort1.sort((a,b) => a.localeCompare(b)));
let arrSort3 = [‘cat’, ‘Been’, ‘ant’, ‘Dog’];
console.log(arrSort3.sort((a,b) => a.localeCompare(b)));

A

To sort el in arr we use
sort() and locateCompare for str in arr
console.log(arrSort1.sort((a,b) => a.localeCompare(b))); [ ‘ant’, ‘been’, ‘cat’ ]

console.log(arrSort3.sort((a,b) => a.localeCompare(b))); [ ‘ant’, ‘Been’, ‘cat’, ‘Dog’ ]

29
Q

Which method is used, what is the output?

let arrObj = [
{name:’John’, salary: 590000},
{name:’John1’, salary: 9190000},
{name:’John2’, salary: 290000},
{name:’John3’, salary: 1390000}
]

console.log(arrObj.sort((x,y) => x.salary - y.salary));

A

Use sort() for arr with objects for numerical property
console.log(arrObj.sort((x,y) => x.salary - y.salary));
[
{ name: ‘John2’, salary: 290000 },
{ name: ‘John’, salary: 590000 },
{ name: ‘John3’, salary: 1390000 },
{ name: ‘John1’, salary: 9190000 }
]

30
Q

Which method is used, what is the output?
let arrObj = [
{name:’John’, salary: 590000},
{name:’John1’, salary: 9190000},
{name:’John2’, salary: 290000},
{name:’Anton’, salary: 1390000}
]
console.log(arrObj.sort((x,y) => x.name.localeCompare(y.name)));

A

Use sort() for arr with objects for string property
console.log(arrObj.sort((x,y) => x.name.localeCompare(y.name)));

[
{ name: ‘Anton’, salary: 1390000 },
{ name: ‘John’, salary: 590000 },
{ name: ‘John1’, salary: 9190000 },
{ name: ‘John2’, salary: 290000 }
]

31
Q

Which method is used, what is the output?
let arrObj = [
{name:’John’, salary: 590000},
{name:’John1’, salary: 9190000},
{name:’Jo’, salary: 290000},
{name:’Ant’, salary: 1390000}
]
console.log(arrObj.sort((x,y) => x.name.length -y.name.length));

A

Use sort() for arr with objects for longest string property
console.log(arrObj.sort((x,y) => x.name.length -y.name.length));
[
{ name: ‘Jo’, salary: 290000 },
{ name: ‘Ant’, salary: 1390000 },
{ name: ‘John’, salary: 590000 },
{ name: ‘John1’, salary: 9190000 }
]

32
Q

Which method is used, what is the output?

let city = [
{name:’Los Angeles’, population: 3800000},
{name:’New York’, population: 90190000},
{name:’Chicago’, population: 3290000},
{name:’Montana’, population: 15390000}
]

let bigCity = city.filter(e => e.population > 4000000 )

console.log(bigCity);

A

Use filter() for arr with objects cityes propertis is biggest then 4 000 000

console.log(bigCity);

[
{ name: ‘New York’, population: 90190000 },
{ name: ‘Montana’, population: 15390000 }
]

33
Q

Which method is used, what is the output?

let cities = [
{name:’Los Angeles’, population: 3800000},
{name:’New York’, population: 90190000},
{name:’Chicago’, population: 3290000},
{name:’Montana’, population: 15390000}
]
cities
.filter(e => e.population > 4000000)
.sort((a,b) => a.population - b.population)
.map(c => console.log(${c.name}:${c.population}) )

A

Use filter() sort() map() for arr with objects cityes propertis is biggest then 4 000 000 sort them and log in special format

cities
.filter(e => e.population > 4000000)
.sort((a,b) => a.population - b.population)
.map(c => console.log(${c.name}:${c.population}) )

Montana:15390000
New York:90190000

34
Q

Which method is used, what is the output?

let data = [11, 22, 53, 4, 5, 6, 7, “cat”, “been”, “ant”];

function isInRang(val) {
if (typeof val !== “number”) {
return false;
}
return val >= this.min && val <= this.max;
}

let rang = {
min: 1,
max: 10,
};

let numInRang = data.filter(isInRang, rang);
console.log(numInRang);

A

Use filter() and function isIn Rang to check only number in rang

console.log(numInRang);
[ 4, 5, 6, 7 ]

35
Q

Which method is used, what is the output?

let circle = [10, 30, 50]
const circleArea =(r) => Math.floor(Math.PIrr)
let areas = circle.map(circleArea)
console.log(areas);

other way the same task

let circle = [10, 30, 50]
let areas = circle.map((r) => Math.floor(Math.PIrr))
console.log(areas);

A

Use map() we have r of circle and must calculate area for each circle

console.log(areas); //[ 314, 2827, 7853 ]

36
Q

Which method is used, what is the output?

let num = [16, 25, 64]
const numSq = num.map(Math.sqrt)
console.log(numSq);

A

use map()
console.log(numSq); [ 4, 5, 8 ]

37
Q

Which method is used, what is the output?
let letter = [‘A’,’B’,’C’]
letter.forEach(e => console.log(e))

A

forEach() iterates over el in arr and execute a predefined func ones for el
console.log(e))
A
B
C

38
Q

Which method is used, what is the output?

let num1 = [20,30, 40]
const sum = num1.reduce((acc,currN) => acc + currN)
console.log(sum);

A

reduce() method have 2 arguments 1.reducer function 2.initial value

reduce method call reduce function for every el of arr

reducer func returns value which is an accumolate result, this result is base for next call
console.log(sum); //90

39
Q

Which method is used, what is the output?
let shopCart = [
{
product:’phone’,
qty:1,
price:699
},
{
product:’Protector’,
qty:1,
price:9.98
},
{
product:’Memory’,
qty:2,
price:20.99
},
]

const total = shopCart.reduce((acc, curr) => {
return acc + curr.qty*curr.price
},0)
console.log(total);

A

Use reduce() to calculate total amount for shoppingCart
console.log(total); //750.96

40
Q

Which method is used, what is the output?

const cssClasses =[‘btn’, ‘btn-primary’, ‘btn-active’]
const btnClass = cssClasses.join(‘ ‘)
console.log(btnClass);

A

Join() concatenate all el of arr and return new str

in this case we use join() - to join css classes
console.log(btnClass);
btn btn-primary btn-active

41
Q

Which method is used, what is the output?

const title = [‘A’,’B’,’C’]
const url = title.join(‘-‘).toLowerCase()
console.log(url);

A

//join() to replace all occurrences of a str and replace whit small letter

console.log(url); //a-b-c

42
Q

What is represented in this task?

let activities = [
[‘Work’, 9],
[‘Eat’, 1],
[‘Commute’, 2],
[‘Play game’, 1],
[‘Sleep’, 7],
]

console.table(activities)

A

Multidimensional Array
console.table(activities)
┌─────────┬─────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼─────────────┼───┤
│ 0 │ ‘Work’ │ 9 │
│ 1 │ ‘Eat’ │ 1 │
│ 2 │ ‘Commute’ │ 2 │
│ 3 │ ‘Play game’ │ 1 │
│ 4 │ ‘Sleep’ │ 7 │
└─────────┴─────────────┴───┘

43
Q

What is represented in this task?

let activities = [
[‘Work’, 9],
[‘Eat’, 1],
[‘Commute’, 2],
[‘Play game’, 1],
[‘Sleep’, 7],
]

activities.push([‘Study’,2])
console.table(activities)

A

Multidimensional Array with push

console.table(activities)

┌─────────┬─────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼─────────────┼───┤
│ 0 │ ‘Work’ │ 9 │
│ 1 │ ‘Eat’ │ 1 │
│ 2 │ ‘Commute’ │ 2 │
│ 3 │ ‘Play game’ │ 1 │
│ 4 │ ‘Sleep’ │ 7 │
│ 5 │ ‘Study’ │ 2 │

44
Q

What is represented in this task?

let activities = [
[‘Work’, 9],
[‘Eat’, 1],
[‘Commute’, 2],
[‘Play game’, 1],
[‘Sleep’, 7],
]

activities.splice(2,0,[‘Programming’,4])
console.table(activities)

A

Multidimensional Array with splice

console.table(activities)

┌─────────┬───────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼───────────────┼───┤
│ 0 │ ‘Work’ │ 9 │
│ 1 │ ‘Eat’ │ 1 │
│ 2 │ ‘Programming’ │ 4 │
│ 3 │ ‘Commute’ │ 2 │
│ 4 │ ‘Play game’ │ 1 │
│ 5 │ ‘Sleep’ │ 7 │
│ 6 │ ‘Study’ │ 2 │
└─────────┴───────────────┴───┘

45
Q

What is represented in this task?

let activities = [
[‘Work’, 9],
[‘Eat’, 1],
[‘Commute’, 2],
[‘Play game’, 1],
[‘Sleep’, 7],
]
activities.forEach(a=>{
let percentage = ((a[1]/24)*100).toFixed()
a[2] = percentage + ‘%’
})
console.table(activities)

A

with forEach calculate % of the hours spend to each activity and appends % to the inner arr

console.table(activities)

┌─────────┬───────────────┬───┬───────┐
│ (index) │ 0 │ 1 │ 2 │
├─────────┼───────────────┼───┼───────┤
│ 0 │ ‘Work’ │ 9 │ ‘38%’ │
│ 1 │ ‘Eat’ │ 1 │ ‘4%’ │
│ 2 │ ‘Programming’ │ 4 │ ‘17%’ │
│ 3 │ ‘Commute’ │ 2 │ ‘8%’ │
│ 4 │ ‘Play game’ │ 1 │ ‘4%’ │
│ 5 │ ‘Sleep’ │ 7 │ ‘29%’ │
│ 6 │ ‘Study’ │ 2 │ ‘8%’ │
└─────────┴───────────────┴───┴───────┘

46
Q

What is represented in this task?

let activity = [
[‘Work’, 9],
[‘Eat’, 1],
[‘Commute’, 2],
[‘Play game’, 1],
[‘Sleep’, 7],
]

activity.pop()
console.table(activity)

A

Remove el from Multidimensional Array
console.table(activity)
┌─────────┬─────────────┬───┐
│ (index) │ 0 │ 1 │
├─────────┼─────────────┼───┤
│ 0 │ ‘Work’ │ 9 │
│ 1 │ ‘Eat’ │ 1 │
│ 2 │ ‘Commute’ │ 2 │
│ 3 │ ‘Play game’ │ 1 │