I am practicing Data Structures on HackerRank and i came across a question which says to implement a queue using two stacks so i looked up for it and understood the logic which was quite simple and it was like : Make two stacks Enqueue And Dequeue : for enqueue operation push element in Enqueue stack : for dequeue operation -first check if the Dequeue stack is empty or not ,if empty then pop all elements from Enqueue stack and push them into Dequeue stack so that the Enqueue is totally reversed and now we can pop the top from the Dequeue stack to perform dequeue operation(return the front of the queue) :and if the Dequeue stack is not empty then just pop from that same stcak that would be the front of the queue. below is the an example for loop that transfers all the content of the Enqueue stack and and puts them in Dequeue stack using push and pop operation but the for loop is behaving oddly
let enqueueStack = [1,2,3,4,5,6]
let dequeueStack = [];
for(let i=0;i<enqueueStack.length;i++){
dequeueStack.push(enqueueStack.pop())
}
console.log(enqueueStack)
console.log(dequeueStack
and below is the output
[ 1, 2, 3 ]
[ 6, 5, 4 ]
i tried forEach which is also not working