Skip to content

Instantly share code, notes, and snippets.

@iJKTen
Last active June 13, 2020 21:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iJKTen/7c8079592ca998306e989984fd63ae51 to your computer and use it in GitHub Desktop.
Save iJKTen/7c8079592ca998306e989984fd63ae51 to your computer and use it in GitHub Desktop.
// Declare an array without a size
const arr = [];
// Declrate an array with a size
const arr2 = new Array(8);
console.log({"typeOfArr": typeof arr});
console.log({"typeOfArr2": typeof arr2});
// Populate array arr
arr.push(1);
arr.push(2);
arr.push(3, 4);
arr.push(5);
arr.push(6, 7, 8);
// Populate array arr2
arr2.push(1);
arr2.push(2);
arr2.push(3, 4);
arr2.push(5);
arr2.push(6, 7, 8);
arr2.push(9);
console.log({first: arr[0]});
console.log({second: arr[1]});
console.log({third: arr[2]});
console.log({fourth: arr[3]});
console.log({fifth: arr[4]});
console.log({sixth: arr[5]});
console.log({seventh: arr[6]});
console.log({eighth: arr[7]});
for (let index = 0; index < arr.length; index++) {
console.log({index: index, value: arr[index]});
}
arr.forEach((value, index) => {
console.log({index: index, value: arr[index]});
});
for (const value of arr) {
console.log({value: value});
}
// while loop
let index = 0;
while (index < arr.length) {
console.log({index: index, value: arr[index]});
index++;
}
// do while loop
index = 0;
do {
console.log({index: index, value: arr[index]});
index++;
} while (index < arr.length)
const factorial = (n) => {
if (n > 1) {
return n * factorial(n - 1);
}
return n;
}
const result = factorial(4);
console.log({result});
class Stack {
constructor() {
this.data = [];
}
push(n) {
this.data.unshift(n);
}
top() {
return this.data[0];
}
pop() {
return this.data.shift();
}
toString() {
return this.data;
}
}
const stack = new Stack();
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(40);
console.log(stack.toString());
console.log(stack.top());
console.log(stack.pop());
console.log(stack.toString());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment