Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript reverse alphabet

so I've been messing around with the .replace() function lately and wanted to make it reverse whatever the user inputs. (Aka a -> z, A -> Z, b -> y, B -> Y, ...)

I'm using function stacking, so I just added .replace().replace()... for every letter, but of course that won't work since whenever it hits n, it will begin to reverse all the progress and I end up with an inaccurate translation. Any idea how I can work around this, since as far as I know, JS doesn't have a .reverse() function like Python?

In case you need it, here's my code

//replacing letters
lettertext = ttext.replace("a", "z")
.replace("A", "Z")
.replace("b", "y")
.replace("B", "y")
.replace("c", "x")
.replace("C", "X")
.replace("d", "w")
.replace("D", "W")
.replace("e", "v")
.replace("E", "V")
.replace("f", "u")
.replace("F", "U")
.replace("g", "t")
.replace("G", "T")
.replace("h", "s")
.replace("H", "S")
.replace("i", "r")
.replace("I", "R")
.replace("j", "q")
.replace("J", "Q")
.replace("k", "p")
.replace("K", "P")
.replace("l", "o")
.replace("L", "O")
.replace("m", "n")
.replace("M", "N")
.replace("n", "m")
.replace("N", "M")
.replace("o", "l")
.replace("O", "L")
.replace("p", "k")
.replace("P", "K")
.replace("q", "j")
.replace("Q", "J")
.replace("r", "i")
.replace("R", "I")
.replace("s", "h")
.replace("S", "H")
.replace("t", "g")
.replace("T", "G")
.replace("u", "f")
.replace("U", "F")
.replace("v", "e")
.replace("V", "E")
.replace("w", "d")
.replace("W", "D")
.replace("x", "c")
.replace("X", "C")
.replace("y", "b")
.replace("Y", "B")
.replace("z", "a")
.replace("Z", "A")
.replace("ä", "ß")
.replace("Ä", "ẞ")
.replace("ö", "ü")
.replace("Ö", "Ü")
.replace("ü", "ö")
.replace("Ü", "Ö")
.replace("ß", "ä")
.replace("ẞ", "Ä")
like image 580
Aci Avatar asked Dec 10 '22 04:12

Aci


1 Answers

Just create an array consisting of string characters from a - z and another array consisting of all the 8 umlaut characters that you mentioned.

You can now simply create a reusable function say, reverseChar() that accepts a character as a parameter.

The function can then check if the inputted character is an alphabet or an umlaut character using a basic regex tester.

The function then tries to match the inputted character with the string characters from the respective array and if there is a match, return the same indexed character from the array reversed.


Try inputting any character from a-z, A-Z or one of the umlaut characters mentioned above in the code snippet below to see how it works:

var alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"];
var umlauts = ["ä","Ä","ö","Ö","ü","Ü","ß","ẞ"]

var val = "";

var result = document.getElementById("result");

function reverseChar(x) {
    
    if (/^[a-zA-Z]+$/.test(x)) {
      for (i = 0; i < 26; i++) {
          if (x.toLowerCase() == alpha[i]) {
              if (x == x.toUpperCase()) {
                val = ((alpha.reverse())[i]).toUpperCase();
              } else {
                val = (alpha.reverse())[i];
              }
          }
      }
	  result.innerHTML = `The reversed character for <strong>${x}</strong> is <strong>${val}</strong>`;
    } else {
        for (i = 0; i < umlauts.length; i++) {
        	if (x == umlauts[i]) {
            val = (umlauts.reverse())[i];
          }
        }
        result.innerHTML = `The reversed character for <strong>${x}</strong> is <strong>${val}</strong>`;
    }
}

// JavaScript below is for the HTML Example

var btn = document.getElementById("btn");

function checkChar(){
    var char = document.getElementById("char");
    var input = char.value;
    reverseChar(input);
}

btn.addEventListener("click", checkChar);
<input id="char" type="text" maxlength="1" />
<button type="button" id="btn">Check reversed character</button>
<p id="result"></p>
like image 120
AndrewL64 Avatar answered Jan 03 '23 15:01

AndrewL64