No repeats please -Loop skipping elements

No repeats please -Loop skipping elements
0.0 0

#1

Hi, I have got a very peculiar problem. The loop is skipping only the second element of the string (I have tried “abc”, “abcd”, “abcde”, “adcedef” etc.) Otherwise it works fine. Can someone tell me what’s happening?

P.S. I know this is a very inefficient way to approach the challenge, but I wanted to use my own code till I could understand Heap’s algorithm.

Your code so far

  if(str.length==1){
    return 1;
  }else if(/^(.)\1+$/.test(str) ){
    return 0;
  }
  var test=[1];
  function addNew(arr,char){
    var result =[];
    for(i=0;i<=arr.length;i++){
      var temp =[...arr];
      temp.splice(i,0,char);
      result.push(temp);
    }
    return result;
  }
  
  var chars =str.split("");
  var result =[[]];
  for(i=0;i<chars.length;i++){
   var temp =[...result];
   result =[];
   for(j=0;j<temp.length;j++){
     result =[...result,...addNew(temp[j],chars[i])];
   } 
   
  }
  
  
  return result;
}

permAlone('acd');

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36.

Link to the challenge: