The global variable result is reused for all test cases without being reinitialized - best to avoid global variables - turn it into a local variable of some function - use console.log to see whatβs in result
maybe fcc isnt accepting the result global variable .so i tried putting it inside the functions and modified the code a bit it passed the test cases .here is the code:
function doPerm(prefix, suffix, result) {
if (suffix.length === 0)
{
if(!checkForRepeat(prefix))
{
result.push(prefix);}}
else {
for (var i = 0; i < suffix.length; i++) {
doPerm(prefix + suffix.charAt(i), suffix.slice(0, i) + suffix.slice(i + 1), result);
}
}
return result;
}
function checkForRepeat(alphabetString)
{
var reg=/(\w)\1+/g;
return reg.test(alphabetString);
}
function permAlone(str) {
var prefix = "";
var suffix = str;
var result=[];
result=doPerm(prefix, suffix, result);
return result.length;
}
permAlone("aab");
i have taken off the result variable and declared it inside the permalone function and assigned the return value of the doperm function to the resut variable.
there could be any other reason too i am pretty new to this .