Help - Advanced Alogrithm Scripting - No Repeats Please

Hi, I hope someone can give me an idea of where I am going wrong here with this challenge.

For now I am just trying to build an array of all possible combinations of the characters in the passed string. I don’t know if the issue I am having is to do with losing the scope of the arr variable but was under the impression if I passed and returned it with each function call (recurse function) that I would essentially have all the values returned to the original function call. All that is left in arr is ‘aab’ when the original call returns.

This is my code:

function recurse(str,currStr,arr,fullLen){
  if (currStr.length===fullLen){
    arr.push(currStr);
  }
  else {
    for (i=0;i<str.length;i++){
      var tempStr="";
      if (i===0 && str.length>1)
        tempStr=str.slice(1);
      else if (str.length===1)
        tempStr="";
      else if (i===(str.length-1))
        tempStr=str.slice(0,-1);
      else
        tempStr=str.slice(0,i)+str.slice(i+1);
      
      arr=recurse(tempStr,currStr+str[i],arr,fullLen);
    }
  }
  return arr;
}

function permAlone(str) {
  var arr=[];
  arr=recurse(str,"",arr,str.length);
  //console.log(arr);
  return arr;
}

permAlone('aab');

You are almost there as it seem to be working! ( ignore my edits please, it was just to keep my editor from complaining )

click to enlarge…

1 Like

Oh wow, how silly of me. I’m guessing not declaring the ‘i’ variable correctly was breaking it. Thanks a lot Rick. I don’t believe I would have picked that up for a good while. Appreciate it! :smile:

You’re Very Welcome Nikola…