JS Array Flashcards
What is array in JS
Array is an ordered list of values /element whit index/
Array Characteristics
1.Array can hold values of different typs
2.Array is auto-growing, the length is dinamicaly sized
How create arr
let arr = newArray() //empty arr
let arr1 = newArray(10) //- array whit size 10
let arr2 = [el1, el2, el3]
Find array length
let arr3 = [‘el1’, ‘el2’, ‘el3’]
let arr3 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr3.length); //3
Add el in the end of arr
let arr4 = [‘el1’, ‘el2’, ‘el3’]
let arr4 = [‘el1’, ‘el2’, ‘el3’]
arr4.push(‘el4’)
console.log(arr4); //[ ‘el1’, ‘el2’, ‘el3’, ‘el4’ ]
add el in the begin of arr
let arr5 = [‘el1’, ‘el2’, ‘el3’]
let arr5 = [‘el1’, ‘el2’, ‘el3’]
arr5.unshift(‘el5’)
console.log(arr5); //[ ‘el5’, ‘el1’, ‘el2’, ‘el3’ ]
// removing el in the end of arr
let arr6 = [‘el1’, ‘el2’, ‘el3’]
let arr6 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr6.pop() //el3
console.log(arr6); //[ ‘el1’, ‘el2’ ]
removing el in the begin of arr
let arr7 = [‘el1’, ‘el2’, ‘el3’]
let arr7 = [‘el1’, ‘el2’, ‘el3’]
console.log(arr7.shift() //el1
console.log(arr7); //[ ‘el2’, ‘el3’ ]
// Find index el of arr
let arr8 = [‘el1’, ‘el2’, ‘el3’]
el2
console.log(arr8.indexOf(‘el2’)) //1
Check is arr
let arr9 = [‘el1’, ‘el2’, ‘el3’]
console.log(Array.isArray(arr9)) //true
What is the Stack?
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/.
What is result?
let stack = [1,2,3]
stack.push(4)
console.log(stack);
push() add one or more el in top of stack /end/
console.log(stack); //[ 1, 2, 3, 4 ]
What is result?
let stack1 = [1,2,3]
console.log(stack1.pop())
console.log(stack1);
pop() remove element of top of the stack /end arr/
console.log(stack1.pop()) //3
console.log(stack1);//[ 1, 2 ]
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”));
reverse str with JS stack
console.log(reverse(“Lia”)); //aiL
What is the Queue?
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
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);
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 ]
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);
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
]
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);
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 ]