Unresponsive script warning.
When I run this script my browser hangs-up. When I try same script running on https://repl.it/languages/javascript I get expected results. However not on FreeCodeCamp site. It might be only an Ubuntu issue, however I thought that is worth it to submit just in case someone else has the same issue.
If you change" i < n"; to "i < n-1" in generate function for permutations script will run however it doesn't generate right amount of permutations.
Challenge No repeats please has an issue.
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0.
Please describe how to reproduce this issue, and include links to screenshots if possible.
My code:
function permAlone(str) {
str = generate(str);
function generate(string){
var arr = string.split('');
var permutations = [];
function swap(a, b){
var tmp = arr[a];
arr[a] = arr[b];
arr[b] = tmp;
}
function generate(n){
if(n===1){
permutations.push(arr.join(''));
} else {
for (var i = 0; i < n; ++i){
generate(n-1);
swap(n % 2 ? 0 : i, n-1);
}
//generate(n-1);
}
}
generate(arr.length);
return permutations;
}
return str;
}
permAlone('abc');
@raisedadead Can we add help wanted label to this. Its a valid issue and I can replicate it.
The problem is repl.it runs the JavaScript on a dedicated container on server side. We do it in browser. So there is a bit of a limitation.
@ajain17 do you have any suggested fixes in mind?
Hi,
I don't, at the moment. I can try and work on this over the weekend if we do not find help till then. Too swamped with work and trying to triage quick issues for now.
The algorithm at hand is O(n!) and @the test case for 'zzzzzzzz' is 8 characters long, causing the browser to hang while the browser keeps trying to swap the z's. The challenge itself seems to be correct. The code here does not cause an infinite loop. It would be possible to add timeouts to the test function but that is another issue.
Most helpful comment
Hi,
I don't, at the moment. I can try and work on this over the weekend if we do not find help till then. Too swamped with work and trying to triage quick issues for now.