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

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

#1

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;

}


#2

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


#3

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 .


#4

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


#5

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