arrays Flashcards
what are the two ways of creating arrays in js
literal using square brackets [] and using new Array()
what is an array constructor?
The “Array Constructor” refers to a method of creating arrays by invoking the Array constructor function aka new array()
what are some differences between using the [] and new array()
[] is usually faster more readable and used more and when you do
let array = [3]; it will create an array with the value 3 only
while with using array constructor
let array = new array(5) ; it will create an array with 5 slots with no values so its usually better for creating an array with a specific size
how can you access the last and first values
assigning them to a name
let array = [1, 2, 3] ;
let firstInArray = array[0] -> access the value in index 0 which is first
let lastInArray = array[array.length - 1] -> array.length -1 give last in array since index count starts from 0 not 1
how can you modify or overwrite a value in an array
first you access the value you want to modify then overwrite it
array[3] = “new value”;
what does these do
push()
unshift()
pop()
shift()
push() to add at the end on an array
unshift() to add at the beginning on an array
pop() to remove at the end
shift() to remove from the beggining
how does slice() work
The slice() method of Array instances returns a shallow copy of a portion of an array into a new array object selected from start to end
const animals = [“ant”, “bison”, “camel”, “duck”, “elephant”];
console.log(animals.slice(2));
// Expected output: Array [“camel”, “duck”, “elephant”]
console.log(animals.slice(2, 4));
// Expected output: Array [“camel”, “duck”]
How can you increase or decrease an array length
Let array = [1, 2,3,4];
To increase
array.length = 5 -> [1,2,3,4,emptyslot]
To decrease
array.length = 2 -> [1,2]
Array are fundamental to build _____
Other data structures
What are some way to iterate an array
Using a for loop
let a = [10, 20, 30, 40, 50];
for (let item of a) {
console.log(item);
Using forEach()
let a = [10, 20, 30, 40, 50];
a.forEach((item) => {
console.log(item);
});
How can you combine 2 arrays
Using concat() which returns a new array
const array1 = [“a”, “b”, “c”];
const array2 = [“d”, “e”, “f”];
const array3 = array1.concat(array2);
console.log(array3);
// Expected output: Array [“a”, “b”, “c”, “d”, “e”, “f”
Does toString() work on an array if so what does it do
Yes, it turns an array into a string
Let array =[“hello”, “dear”]
Console.log( array.toString() );
How can you check if sometjing is an array in js
There are three methods tell me which works and which doesnt
Let ourarray= [1,2,3,4];
Typeof(ourarray) doesnt work on an array it returns a object
Array.isArray() works
Console.log(Array.isArray(ourarray) ); returns true
Instanceof works
Console.log ( ourarray instanceof Array ); returns true
WHAT DOES “EVERY-NTH ELEMENT” “EVERY-Kth ELEMENT”
MEAN?
Let’s say we got an array like:
[5, 10, 15, 20, 25, 30, 35]
Every 1st element: [5, 10, 15, 20, 25, 30, 35] (no skipping)
Every 2nd element: [5, 15, 25, 35] (we skip 1 each time)
Every 3rd element: [5, 20, 35] (we skip 2 each time)
Every 4th element: [5, 25]
See the pattern? You pick an element, then skip some, then pick again.
How do you print every alternate (every 2nd) element of an array using a loop?
for (let i = 0; i < arr.length; i += 2) {
console.log(arr[i]);
}
How do you recursively print every alternate element in an array?
function printAlternates(arr, i = 0) {
if (i >= arr.length) return;
console.log(arr[i]);
printAlternates(arr, i + 2);
}
How do you create a new array of every 3rd element using a loop?
let res = [];
for (let i = 2; i < arr.length; i += 3) {
res.push(arr[i]);
}
How do you recursively get every 3rd element and return them as a new array?
function getEveryThird(arr, i = 2, res = []) {
if (i >= arr.length) return res;
res.push(arr[i]);
return getEveryThird(arr, i + 3, res);
}
How do you double every 2nd element of an array using a loop?
for (let i = 1; i < arr.length; i += 2) {
arr[i] *= 2;
}
How do you recursively double every 2nd element in-place?
function doubleEverySecond(arr, i = 1) {
if (i >= arr.length) return;
arr[i] *= 2;
doubleEverySecond(arr, i + 2);
}
What’s the basic structure of a recursive function that processes an array?
function recurse(arr, i = 0) {
if (i >= arr.length) return;
// do something with arr[i]
recurse(arr, i + k); // k = how much you want to skip
}
What’s the generic pattern to loop through an array by steps of k from index s?
for (let i = s; i < arr.length; i += k) {
// arr[i]
}
When is recursion better than a loop?
When the problem is naturally recursive (e.g. trees, backtracking)
When you want cleaner code for problems that branch
When you’re flexin’ on interviews
What happens if you forget the base case in recursion?
You cause a stack overflow and crash your program. Always add:
if (i >= arr.length) return;