0

The "for" clause doesn't break even if the first "if statement" enters

function getNodeByName(array, name) {
      var mediaNode = null;
      for (var i = 0; i < array.children.length; i++) {
        if(array.children[i].name === name) {
          mediaNode = array.children[i];
          console.log(array.children[i]);
          break;
        }
        if(array.children[i].children !== null) {
          getNodeByName(array.children[i], name);
        }
      }
      return mediaNode;
    }
  • You could replace `break` with `return mediaNode;` if the return statement immediately follows the for loop. – dragonfire Apr 16 '17 at 17:31
  • 1
    no chance, if control passes inside the first if statement it will jump out of the loop and return the variable at the bottom. – Ousmane D. Apr 16 '17 at 17:31
  • How is this function called? Perhaps the error lies there. – dragonfire Apr 16 '17 at 17:32
  • Your recursive call of the function is useless because you don't handle the return value. Change it to `return getNodeByName(array.children[i], name);`. I suspect that is really causing the bug you are seeing. – Lin Meyer Apr 16 '17 at 17:38

0 Answers0