First time asking a question here. I’ve just completed the No Repeats Please challenge, but spent a lot of time trying to figure out why I couldn’t use the .test method to complete my algorithm, but .search worked.
Here’s my working solution with the .search method.
function permAlone(str) {
let doubleRegEx = /(\w)\1+/g;
function generate(string, permutation = '', permutations = []){
if (!string){
permutations.push(permutation);
return;
}
for (let i = 0; i < string.length; i++){
generate(string.slice(0, i) + string.slice(i + 1), permutation + string[i], permutations);
}
return permutations;
}
return generate(str).filter(x => x.search(doubleRegEx) == -1).length;
}
permAlone('aab');
At first, I thought .test made more sense, but filter kept passing some elements that should have matched the regular expression.
return generate(str).filter(x => x..filter(x => !doubleRegEx.test(x)).length;
Can anyone help me?