Premutations
Making and checking for permutations
by torggya
JavaScript
function permAlone(str) {//infinite loop if str is a single letter
var arra = str.split('');
var n = factorial(str.length);
var regEx = /[^\w\s]|(.)(?=\1)/g; //captures any character set that is being repeated.
var counter = 0;
var counterFalse = 0;
var j = 0;
Array.prototype.swap = function(x, y){
var temp = this[x];
this[x] = this[y];
this[y] = temp;
return filterIt(this);
//filterIt(this[x].concat(this[y]));
}//data.swap(x, y);
/*if (str.length <= 1){
return 1;
};*/
while (j != n){
for (i=0; i < str.length-1; i++){//2
arra.swap(i, i+1);//a > b, a> a, return baa. On j = 1, a > a, a > b. aba, etc
j += 1;
}
}
console.log(counter, counterFalse);
return counter;
function factorial(num){
// If the number is less than 0, reject it.
if (num < 0) {
return -1;
}
// If the number is 0, its factorial is 1.
else if (num == 0) {
return 1;
}
// Otherwise, call this recursive procedure again.
else {
return (num * factorial(num - 1));
}
}
function filterIt(data){//problem: when checking for charactrs that are the same,this still throws both true and false, even if all the characters are the same
var strTest = data.toString().replace(/,/gi, "");
console.log(strTest);
if(regEx.test(strTest) === true){
counter += 1;
} else {
counterFalse += 1;
}
}
}
permAlone('aba');
permAlone('zzz');