Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
.pop() method deletes the element but returns undefined instead of the element itself. (JS Stack implementation)

My function is a recursive function to delete the middle element of a stack. The coding platform has already given it's implementation of a stack which means I have access to the push() and pop() method.

I want to pop() till I reach the middle ele and then push the popped elements back.

However, if I store the pop, it shows up as undefined.

If I console.log the pop call, it returns undefined.

class Solution 
{
    solve(s,midEle) {
        let self = this;
        if(midEle===1){
            s.pop();
            return;
        }
        let temp = s.pop();
        self.solve(s,midEle-1);
        s.push(temp)
        return
    }
    //Function to delete middle element of a stack.
    deleteMid(s, sizeOfStack)
    {
        // code here
        const mid = Math.ceil((sizeOfStack+1)/2);
        this.solve(s,mid);
    
    }
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It is because you are returning nothing:

return;

In javascript, the value of nothing is undefined. Thus the code above returns the value undefined.

To return the value you popped from the array, simply return it:

var item = s.pop();
return item;

or more simply:

return s.pop();
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!