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

192
Vistas
How pop() work inside this if condition statement

In the code below

function validBraces(braces) {
  var matches = { '(': ')', '{': '}', '[': ']' };
  var stack = [];
  var currentChar;

  for (var i = 0; i < braces.length; i++) {
    currentChar = braces[i];
    if (matches[currentChar]) {
      stack.push(currentChar);
    } else {
      if (currentChar !== matches[stack.pop()]) {
        return false;
      }
    }
  }
  return stack.length === 0;
}

To my understanding, this code

currentChar !== matches[stack.pop()]

check that current char is the necessary opening braces of last element in the stack array and if it is correct it pop the last element in stack array. but in the syntax, there is no condition if it matches, like this

if(currentChar == matches[stack.pop()]
{stack.pop()} 

So, is stack.pop() working inside the if condition statement despite there is no true condition for it. How exactly is it working here?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

The expression is evaluated from inside out.

pop removes the last element from the array and returns the removed value. And this character (because stack contains only characters) is used to access the respective property in the matches object.

You can rewrite the code as follows:

if (currentChar !== matches[stack.pop()]) {
  return false;
}

is equivalent to

let stackElement = stack.pop();
let match = matches[stackElement];
if (currentChar !== match) {
  return false;
}

EDIT regarding your comment

No returning true from this position in the code doesn't have anything to do with the "characteristic" of pop (whatever you mean by that).

The whole function is a check for balanced braces. And that specific part of the code checks, whether the current char is the matching equivalent of the current element on the stack. If that's not the case, the braces are not correctly matched, thus you can immediately return false. But you cannot return true at this position, because to be able to determine, that the braces are correctly matched, you have to examine the whole input string. Thus, the last line of the function

return stack.length === 0;

where you check, after the whole string as been handled, if all braces have been correctly matched. If stack.length === 0 gives false, there are still unmatched braces on the stack, thus the result of the function is false. If stack.length === 0 gives true, there is no unmatched brace left, thus the result of the function is true

about 4 years ago · Juan Pablo Isaza Denunciar

0

stack.pop() is executed before evaluation of the expression. It would be identical to the following code:

// ...

const lastEncounteredOpeningBracket = stack.pop();
const requiredClosingBracket = matches[lastEncounteredOpeningBracket]

if (currentChar !== requiredClosingBracket ) {
  return 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