简体   繁体   中英

Can any one explain why i am getting undefined output on this leetcode problem?

back again with another leetcode issue. i cant get my functions or my for each loop to return a value, when I do so it still shows up as undefined

/**
 * @param {string} s
 * @return {boolean}
 */
var isValid = function(s) {
   
  let  opposingParens = {
    "(" : ")",
    "[" : "]",
    "{" : "}"
  }
  let splitChars = s.split('');
   
  let isTruthy = false
  let opposingParensFunc = (paren) => {
      
    if(splitChars[1] == opposingParens[paren] || splitChars[splitChars.length -1] == opposingParens[paren]) {
      isTruthy = true
    }
    return isTruthy
  }
   
       
  splitChars.forEach((char,i) => {
    return opposingParensFunc(char)
  })
};
  1. Your input "()"
  2. Output undefined
  3. Expected true

Thank you for your help.

var isValid = function(s) {

let  opposingParens = {
    "(" : ")",
    "[" : "]",
    "{" : "}"
}
let  splitChars = s.split('');

let isTruthy = false
let opposingParensFunc = (paren) => {
  
   if(splitChars[1] === opposingParens[paren] || splitChars[splitChars.length -1] === opposingParens[paren]  ){
    isTruthy = true
}
   return isTruthy
}
splitChars.forEach((char,i) =>{
    console.log(opposingParensFunc(char))
})  
};
isValid("()")

Like it was said earlier in the comments forEach() returns undefined and the expected answer here would be 2 true because forEach() is running twice.

Your function has no return statement. The one that you do have is providing a return value for another function: the function that you provide to forEach . But that is not determining the return value of the main function. It looks like you wanted to do the following outside any callback function:

return isThruthy;

NB: providing a return value in a forEach callback is useless, that value is never used by it.

However, your algorithm will not work in the general case, because you always compare a character with the one at index 1 and with the last character in the input. But note that the balancing closing bracket could be anywhere in the input. Think of "([]){}".

What you really need, is a stack. When you find an opening bracket, but its closing counter part on the stack, which indicates what you expect to find somewhere at the right of this opening bracket. If there is another opening bracket, then that one should be closed first, so we also push the corresponding closing bracket on the stack, ...etc.

When you find a closing bracket, you compare it with the one on the top of the stack. If they differ, then the input is invalid. If equal, you have a balancing pair, and can pop that info from the stack, ...etc.

When you reach the end of the input, you must also make sure that there are no more closing brackets that were expected. In other words, at that moment the stack should be empty.

Here is an implementation:

let brackets = {
    "(": ")",
    "[": "]",
    "{": "}",
};

var isValid = function(s) {
    let stack = [];
    for (let i = 0; i < s.length; i++) {
        let ch = s[i];
        let open = brackets[ch];
        if (open) stack.push(open);
        else if (ch !== stack.pop()) return false;
    }
    return !stack.length; // true when stack is empty, false otherwise
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM