Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

158
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda