Array.length is 0 but array is bigger than 0

Array.length is 0 but array is bigger than 0
0

#1

Tell us what’s happening:
When I return noRepeat.length it is always 0 even though when I return noRepeat itself, the array returns just fine.

Your code so far

function permAlone(str) {
  var result = [];
  if (str.length == 1) {
    result.push(str);
    return result;
  }
  for (var i = 0; i < str.length; i++) {
    var baseChar = str[i];
    var otherChars = str.substring(0, i) + str.substring(i + 1);
    var innerPerm = permAlone(otherChars);
    for (var j = 0; j < innerPerm.length; j++) {
      result.push(baseChar + innerPerm[j]);
    }
  }
  var regex = /(.)\1+/g;
  var noRepeat = result.filter(function(string) {
    return !string.match(regex);
  });
  return noRepeat.length;
}

permAlone('aab');

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (X11; CrOS x86_64 10032.86.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.140 Safari/537.36.

Link to the challenge:


#2

@cornielleandres,
you have to debug your code since only then will you understand its behavior. I can suggest you look at all the places you wrote return. Try to write out what the code does with the example you provided and you will see what goes wrong.

Tip: take notice that in every place you wrote return, you will eventually come back to that line of code(since you are recursively activating the function).