Home > Back-end >  How to replace an inputted string with values from two different arrays javascript
How to replace an inputted string with values from two different arrays javascript

Time:09-17

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 ;
    
}

CodePudding user response:

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"));

CodePudding user response:

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 ;

}

CodePudding user response:

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.

CodePudding user response:

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'));

CodePudding user response:

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!'))

  • Related