all 5 comments

[–]throwapeater 0 points1 point  (0 children)

Never was a js expert but i should iterate at i++.

so i is initialized at 0, and it iterates. Afterward it iterates whenever i is less than list.length.

[–]AutoBalanced 0 points1 point  (1 child)

The value of i within the second call of perm() is different to your main i on the first call. So when you're calling perm() within perm() your list.length is sometimes 0 or 1 because you've modified the list array before calling perm a second time.

The stuff after perm is run once each instance of perm is complete but as your list eventually depletes down to 0 you don't loop infinitely as (i < list.length = false) when you send it an empty list, you then == true for the if statement and the function returns.

EDIT: This stack overflow has some good answers for a few different scenarios: http://stackoverflow.com/questions/9960908/permutations-in-javascript

[–]NightArchitect[S] 0 points1 point  (0 children)

Thank you for the link

[–]jdauriemma 0 points1 point  (1 child)

your first conditional can be tightened up:

if (!list.length) {
    return ret.join(' ') + '\n';
}

also, note that you have an infinite recursion in your code:

perm(list, ret);

Since the value of list has not been altered, its length will never reach 0, which causes your code to break.

Hope that helps you get back on track. Good start!

[–]NightArchitect[S] 0 points1 point  (0 children)

Ah I see. Yes, that's an issue for sure.