5

Here is my code:

function permAlone(string) {
  if (string.length < 2) return string; // This is our break condition

  var permutations = []; // This array will hold our permutations

  for (var i = 0; i < string.length; i++) {
    var character = string[i];

    // Cause we don't want any duplicates:
    if (string.indexOf(character) != i) // if char was used already
      continue; // skip it this time

    var remainingString = string.slice(0, i) + string.slice(i + 1, string.length); //Note: you can concat Strings via '+' in JS

    for (var subPermutation of permAlone(remainingString))
      permutations.push(character + subPermutation);

  }

  var permutationsFinal = [];
  for (var j = 0; j < (permutations.length); j++) {
    if (!(permutations[j].match(/([a-zA-Z])\1/))) {
      permutationsFinal.push(permutations[j]);
    }
  }

  return (permutationsFinal.length);
  //                       ^^^^^^^^ if I add this, the error is thrown
}

permAlone('abc');

If I replace:

return (permutationsFinal);

by:

return (permutationsFinal.length);

I get this error in the console:

TypeError: permAlone is not a function

Why?

Thanks for your help! :)

12
  • 2
    My guess: It is a recursive function, if you return anything other than what is expected by the function itself then you will break the recursive loop. I only say guess because I am not 100% sure what you are trying to do. Commented Jun 28, 2017 at 3:26
  • Ah crap! You're so right! I forgot about that part! I need to divide this function in two different functions. Thanks for your help! Commented Jun 28, 2017 at 3:28
  • That's so weird, I didn't even know that would happen. Good question! Commented Jun 28, 2017 at 3:29
  • 2
    for (var subPermutation of permAlone(remainingString)) iterates over the return value of the function (called recursively). It's the line number of the error. Numbers are not iterable, so when you return a number instead of an array it throws an error. On that line number. No mystery. Commented Jun 28, 2017 at 3:37
  • 1
    @ibrahimmahrir Nice, that makes me much happier :P Commented Jun 28, 2017 at 3:55

2 Answers 2

3

It is a recursive function, if you return anything other than what is expected by the function itself then you will break the recursive loop.

Sign up to request clarification or add additional context in comments.

Comments

3

To remove an answer from comment:

for (var subPermutation of permAlone(remainingString)) iterates over the return value of the function (called recursively). It's the line number of the error. Numbers are not iterable, so when you return a number instead of an array it throws an error.

Not sure what browser you are using, but FireFox reports the error as

TypeError: permAlone(...) is not iterable

which is more or less self explanatory. If the error message reported in your browser is for the code posted, it is arguably not just misleading but factually incorrect.

1 Comment

Just so it is known, chrome create the same error the OP described: permAlone is not a function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.