'use strict';
/*
* IMPLEMENT FEW ARRAY METHODS LIKE get() , push() , delete() ,shift() , unshift() ,get()
* Arr is javascript is object with just key and value so each index of item is key for elemenet.
*/
class MyArray {
constructor(){
this.data = {};
this.length = 0;
}
//get length of arr O(1)
getLength(){
return this.length;
}
//Add data in array at last
push(item){
this.data[this.length] = item;
this.length++;
return this.length;
}
//remove last item from array O(1)
pop(){
if(this.length === 0) return
let lastItem = this.data[this.length-1];
delete this.data[this.length-1];
this.length--
return lastItem;
}
//get item from array O(1)
get(index){
if(index < this.length){
let item = this.data[index]
return item
}
return -1;
}
//add item at first place O(n)
// Must understand why we need loop from back side spent 1 hours
unshift(item){
for(let i = this.length ; i >=1 ; i--){
this.data[i] = this.data[i-1]
}
this.data[0] = item
this.length++;
return this.length
}
//remove first item from array O(n)
shift(){
let firstItem = this.data[0]
for(let i=0 ; i<this.length-1;i++){
this.data[i]=this.data[i+1]
}
delete this.data[this.length-1]
this.length--;
return this.data[0]
}
}
let arr = new MyArray();
const arrLength = arr.getLength();
arr.push("T1");
arr.push("T2");
arr.pop()
arr.unshift('adding first value')
arr.unshift('now this will be first')
arr.pop()
arr.get(3)
arr.shift()
arr.shift()
arr.pop()
arr.push('things are working fine')
console.log(arr)
/*
*Reverse a string : We have done with reversing a loop lets see how its done in video (SAME SOLUTION IN VIDEO ALSO)
* Always add inputs conditions first like do we have any value in input or not. (undefiend,null,blank)
*/
function reverse(str) {
if(!str || typeof str !== 'string' ||str.length === 0) return "Kush to galat hai bhai."
let a = ""
for(let i=str.length-1; i>=0;i--){
a+=str[i]
}
return a
}
/*
* We can simply use inbuilt methods and can solve in single line (Not recommended)
*/
function reverseWithInbuilt(str) {
return str.split('').reverse().join('')
}
let reversedString = reverseWithInbuilt("asd")
console.log(reversedString)
/*
* Merge to sorted array : input : [0,2,4,6] [1,3,5,7] => output => [0,1,2,3,4,5,6,7]
* type of [] === object (true) because arr are object
* in while loops or any other loops before you start writting condition thing about edge cases. (ALWAYS)
*/
function mergeTwoSortedArr(arr1,arr2){
//Check values
if(!arr1 || !arr2 || typeof arr1 !== 'object' || typeof arr2 !== 'object' || arr1.length === 0 || arr2.length === 0) return []
let firstArrLength = arr1.length;
let secondArrLength = arr2.length;
let finalArrLength = firstArrLength + secondArrLength
let finalArr = []
let i = 0;
let j =0;
let k =0;
//CONDITION MATTERS (i !== firstArrLength) in this if inside loop we do i+=2 then this will fail
while(i < firstArrLength&& j<secondArrLength){
if(arr1[i] < arr2[j]){
console.log("in i",arr1[i])
finalArr[k]=arr1[i]
i++
}else{
console.log("in j",arr2[j])
finalArr[k]=arr2[j]
j++
}
k++;
}
while(i<firstArrLength){
finalArr[k] =arr1[i]
i++
k++
}
while(j<secondArrLength){
finalArr[k] =arr2[j]
j++
k++
}
return finalArr
}
let output = mergeTwoSortedArr([0,2,14,16,170],[1,3,56,70])
console.log(output)