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

154
Vistas
When will `return !stack.length` ever return `true`?

I am confused why this function would ever return true. If an item is added to the stack, then returning !stack.length would return false, right? I think I am missing something about how the stack works but I cannot seem to find the answer.

var isValid = function (s) {
  const hash = {
    '(': ')',
    '{': '}',
    '[': ']',
  };

  const stack = [];

  for (const char of s) {
    if (char in hash) stack.push(char);
    else {
      const top = stack.pop();

      if (top === undefined || hash[top] !== char) {
        return false;
      }
    }
  }

  return !stack.length;
};
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

That will happen when the length of the stack is equal to 0. Putting a ! in front of stack.length will evaluate NOT 0. The result of this is true.

Take a look at here. Or to be very specific:

If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false

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