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

200
Views
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 answers
Answer question

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 Report

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 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!