+ 2

How to Assign values to each Letter Or Number in JavaScript?

Like eg, var name = prompt (“Type Your Name”) document.write(name); This is how it happens but I want to know how to assign values to each letter, My name is Raaja and I want to assign values to each letter like, R - Rabbit A - Ate J- Jagged So when prompt appears and I type my name Raaja, The result shows up like, “Rabbit Ate Ate Jagged Ate” How do we do that? Im a rookie but I’m curious to know how it’s done! Would be absolutely one heck of a help if u help me! Thanks In Advance 😁🙏

16th Mar 2018, 12:29 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
5 odpowiedzi
+ 15
use an object to map letters to values var letterMapper = { a: "Ate", b: "Boring", c: "Cupid", d: "Drama" ..... } for replacing letter with value just do something like this: var name = "Raaja"; var res = ""; for(var letter of name){ res += letterMapper[letter.toLowerCase()] + " "; }
16th Mar 2018, 12:45 PM
Burey
Burey - avatar
+ 6
np @Raaja 😎
16th Mar 2018, 12:57 PM
Burey
Burey - avatar
+ 3
You can use a two-dimensional array, and assign the letter on the first dimension, and the word on the second. Then you can make a loop that iterates through each letter in the text then match the key to its word value, then insert the output from each letter into a new array. Use another loop to print the elements of that array and insert whitespace between each element. That should pretty much be it.
16th Mar 2018, 12:47 PM
apex137
apex137 - avatar
+ 2
@Burey Wow never thought it would be this Easy! Thanks a lot 😁🙌🏻
16th Mar 2018, 12:50 PM
Raaja Thalapathy
Raaja Thalapathy - avatar
+ 2
@Jericho Arcelao Thanks bro, Think that would help 😁👌🏻
16th Mar 2018, 12:53 PM
Raaja Thalapathy
Raaja Thalapathy - avatar