NO REPEATS PLEASE ! Output to the console is right , But test not passing!

var result = [];

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);
}
}

}
function checkForRepeat(alphabetString)
{
var reg=/(\w)\1+/g;

return reg.test(alphabetString);
}

function permAlone(str) {
var prefix = β€œβ€;
var suffix = str;
doPerm(prefix, suffix, result);

return result.length;

}

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

1 Like

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 .

1 Like

I made it a local variable and the test passed ! Thank you so much for the assistance !

Yes the global variable had been causing the issue! I declared it locally and it passed!! Thank you for the assistance!!