Help - Advanced Alogrithm Scripting - No Repeats Please

Help - Advanced Alogrithm Scripting - No Repeats Please
0

#1

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

#2

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…


#3

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:


#4

You’re Very Welcome Nikola…