N
N
nchur2018-03-06 17:18:48
JavaScript
nchur, 2018-03-06 17:18:48

How to make a translator into "hacker language"?

Translate English text into "hack language" (h4ck3rsp34k)! Many people on the internet like to replace certain letters with numbers that look like them - for example, the number "4" looks like the letter "A", "3" looks like "E", "1" looks like "I", and "0" looks like " O". Although the numbers look more like uppercase letters, we will replace them with lowercase letters. To translate plain text into "hacker language", you need a line with the original text and a new empty line for the result:

var input = "javascript is awesome";
var output = "";

Now use a for loop to loop through all the characters in the original string. When you encounter the letter "a", add "4" to the resulting string. When you meet "e", add "3", when you meet "i", add "1", and when you meet "o", add "0". Otherwise, just append the original symbol to the result. Again, the += operator is great for adding a character to the end of a string. Once the loop is complete, print the resulting string to the console. If the program works correctly, you should see the following: "j4v4scr1pt 1s 4w3s0m3".

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Stalker_RED, 2018-03-06
@nchur

var input = "javascript is awesome leet";
var output = "";
for (let i = 0; i<input.length; i++) {
  let symbol = input[i]
  switch (symbol) {
    case 'a': output += '4'; break;
    case 'e': output += '3'; break;
    case 'i': output += '1'; break;
    case 'o': output += '0'; break;
    case 't': output += '7'; break;
    default: output += symbol;
  }
}
console.log(output)
https://jsfiddle.net/z9o7fg8a/
If you are not yet familiar with the switch construct, you can replace it with an if-else series.
Although I would write something like this:
var input = "javascript is awesome leet";
var replaceList = {
  'a': 4,
  'e': 3,
  'i': 1,
  'o': 0,
  't': 7,
}

var output = input.split('').map(s => replaceList[s] || s).join('')
console.log(output)
https://jsfiddle.net/z9o7fg8a/1/
In general, what's the point in learning if someone else solves the problems for you?

O
otstou, 2019-07-04
@otstou

var input = "javascript is awesome";
var output = "";
for (var i = 0;i< input.length;i++){if(input[i]==="a") {output+="4"}
else if(input[i]==="e") {output+="3"}
else if(input[i]==="i") {output+="1"}
else if(input[i]==="o") {output+="0"}
else {output += input[i]}}
console.log(output);

R
Roman Volodin, 2018-03-06
@cronk

I'm not familiar with C js. Is this how you can do it? Or is it really that bad?

"javascript is awesome".replace(/a/g, "4")
                       .replace(/e/g, "3")
                       .replace(/i/g, "1")
                       .replace(/o/g, "0")
                       .replace(/t/g, "7");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question