How to replace an inputted string with values from two different arrays javascript

Viewed 47

I am trying to update a string to its corresponding values with two arrays

Here is the set up

An array of english characters with corresponding leet characters

let letters = [ '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' ];
let leetChars = ['@', '8', '(', '|)', '3', 'ph', 'g', '#','l', '_|', '|<', '1', "|'|'|", '/\/', '0', '|D', '(,)', '|2', '5', '+', '|_|', '|/', "|/|/'",'><', 'j', '2'];

Given a string I want to be able to return a new string containing the corresponding leet characters of the given original string.

for example 'Multiverse' should be "|'|'||_|1+l|/3|253"

My Approach

I have created a function leetTranslator that accepts a a string(of normal characters) as a parameter.I've declared a variable translation which will hold the new string and i created a loop however i'm struggling to replace the original strings characters with the corresponding leet value.

This is what i have

function leetTranslator (str){
    let translation="";
  
        for (i=0;i<str.length;i++){
            translation=str.replace(/str/gi , letters );
            
        }
        
    return translation ;
    
}
6 Answers

I would use an object instead of the two arrays; then the translation is a simple matter of looking up the value of the letter to be translated:

var leetChars = {
  a: "@", b: "8", c: "(", d: "|)", e: "3",
  f: "ph", g: "g", h: "#", i: "l", j: "_|",
  k: "|<", l: "1", m: "|'|'|", n: "//", o: "0",
  p: "|D", q: "(,)", r: "|2", s: "5", t: "+",
  u: "|_|", v: "|/", w: "|/|/'", x: "><", y: "j",
  z: "2"
}

function leetTranslator(str) {
    let translation = "";
    for (i = 0; i < str.length ; i++){
       translation += leetChars[str[i]] || str[i];
    }
    return translation;
}

console.log(leetTranslator('hello world!'))

Note you can use built-in functions to simplify your code:

var leetChars = {
  a: "@", b: "8", c: "(", d: "|)", e: "3",
  f: "ph", g: "g", h: "#", i: "l", j: "_|",
  k: "|<", l: "1", m: "|'|'|", n: "//", o: "0",
  p: "|D", q: "(,)", r: "|2", s: "5", t: "+",
  u: "|_|", v: "|/", w: "|/|/'", x: "><", y: "j",
  z: "2"
}

const leetTranslator = str =>
  str.split('')
    .map(c => leetChars[c] || c)
    .join('')

console.log(leetTranslator('hello world!'))

you can use Map i think it works well

let map = new Map([
['a', '@'],
['b', '8'],
['c', '('],
//etc.
]);


function leetTranslator (str){
let translation="";

    for (i=0;i<str.length;i++){
        translation=map.get(i);
    }
    
return translation ;

}

I don't think you need a regex, you can just check each letter in turn.

let letters = [ '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' ];
let leetChars = ['@', '8', '(', '|)', '3', 'ph', 'g', '#','l', '_|', '|<', '1', "|'|'|", '/\/', '0', '|D', '(,)', '|2', '5', '+', '|_|', '|/', "|/|/'",'><', 'j', '2'];

function leetTranslator (str){
  return str.split("")
    .map(s => letters.includes(s) ? leetChars[letters.indexOf(s)] : s)
    .join("");
}

console.log(leetTranslator("multiverse"));

To achieve what you require you can use map() to create an array of the leet characters, by matching the index of the current character in the string. Then you can just join('') it together to return the new string:

let letters = ['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'];
let leetChars = ['@', '8', '(', '|)', '3', 'ph', 'g', '#', 'l', '_|', '|<', '1', "|'|'|", '/\/', '0', '|D', '(,)', '|2', '5', '+', '|_|', '|/', "|/|/'", '><', 'j', '2'];

let leetTranslator = str => [...str.toLowerCase()].map(c => leetChars[letters.indexOf(c)]).join('');

let multiverseOutput = leetTranslator('Multiverse');
console.log(multiverseOutput );

Note the use of toLowerCase() here, as your original letters array has no uppercase characters.

Since the array is same length, you can map the two arrays.

let letters = [ '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' ];

let leetChars = ['@', '8', '(', '|)', '3', 'ph', 'g', '#','l', '_|', '|<', '1', "|'|'|", '/\/', '0', '|D', '(,)', '|2', '5', '+', '|_|', '|/', "|/|/'",'><', 'j', '2'];

let mappingObject = {};

letters.forEach((letter, index) => {
  mappingObject[letter] = leetChars[index];
});


function leetTranslator(str) {
  let result = '';
  for (i=0;i<str.length;i++){
    result = result + mappingObject[str[i].toLowerCase()];
  }
  return result;
}

console.log(leetTranslator('Multiverse'));

You can simply achieve this by using Array.forEach() and Array.indexOf() method.

Live Demo (All the descriptive comments has been added in the below code snippet itself) :

let letters = [ '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' ];
let leetChars = ['@', '8', '(', '|)', '3', 'ph', 'g', '#','l', '_|', '|<', '1', "|'|'|", '/\/', '0', '|D', '(,)', '|2', '5', '+', '|_|', '|/', "|/|/'",'><', 'j', '2'];

// This function is used to convert the input string into a leetChars string.
function leetCharsStr(str) {
  // Declare a variable with an empty string
  let result = '';
  // Split the input string and iterate over each character
  str.split('').forEach(c => {
    // Find the index of the passed character 'c' from the letters array.
    const index = letters.indexOf(c.toLowerCase());
    // Concat the mapped leetchar into a result string. 
    result += leetChars[index];
  });
  return result;
}

console.log(leetCharsStr('Multiverse'));

Related