Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
.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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda