Home » Javascript » Trying to make recursive permutations function using diacritics in JavaScript

Trying to make recursive permutations function using diacritics in JavaScript

Posted by: admin August 19, 2018 Leave a comment

Questions:

I have managed to write a simple function which takes an input string, and compares each character to a dictionary object containing diacritic (accented) characters paired with their non-diacritic characters, and then attempts to create a new string for each permutation of that.

However, it is only semi functional. As it stands when I input the string ‘hello world’ the output is:

["hèllo world", "héllo world", "hêllo world", "hëllo world", "hēllo world", 
"hėllo world", "hęllo world", "hellô world", "hellö world", "hellò world", 
"helló world", "hellō world", "hellõ world", "hello wôrld", "hello wörld", 
"hello wòrld", "hello wórld", "hello wōrld", "hello wõrld"]

or if I input ‘some string’ then the output is:

["śome string", "šome string", "sôme string", "söme string", "sòme string", 
"sóme string", "sōme string", "sõme string", "somè string", "somé string", 
"somê string", "somë string", "somē string", "somė string", "somę string", 
"some śtring", "some štring", "some strîng", "some strïng", "some stríng", 
"some strīng", "some strįng", "some strìng", "some striñg", "some strińg"]

however, all that it’s doing is going through each character and replacing the value once, then moving onto the next one.

How do I iterate again for each of these array items to find every possible combination of those? Instead of moving onto the next character and leaving the ones behind the same as the original input string. I need it to have every permutation.

I’ve been up all night trying to figure this out and although I’ve made some progress I’m still stuck. I attempted to create a recursive function calling itself from within itself but this just crashed my browser.

Any help would be amazing 🙂

here is the code I’ve written:

function jig(inputStr) {
  const accents = {
    a: ["à", "á", "â", "ä", "ã", "å", "ā"],
    c: ["ç", "ć", "č"],
    e: ["è", "é", "ê", "ë", "ē", "ė", "ę"],
    i: ["î", "ï", "í", "ī", "į", "ì"],
    n: ["ñ", "ń"],
    o: ["ô", "ö", "ò", "ó", "ō", "õ"],
    s: ["ś", "š"],
    u: ["û", "ü", "ù", "ú", "ū"],
    y: ["ÿ"],
    z: ["ž", "ź", "ż"]
  };

  function hasAccents(char) {
    return /[aceinosuyz]/.test(char);
  }

  var results = [];

  for (var i = 0; i < inputStr.length; i++) {
    var currentChar = inputStr.substr(i, 1);
    // console.log(currentChar);
    if (hasAccents(currentChar)) {
      // console.log(accents[currentChar]);

      for (var y = 0; y < accents[currentChar].length; y++) {
        var tempArray = inputStr.split("");

        tempArray[i] = accents[currentChar][y];
        results.push(tempArray.join(""));
        //jig(tempArray.join(""));
      }
    }
  }

  return results;
}
Answers: