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

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

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