Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How pop() work inside this if condtion statement

In the following code

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()]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading