How to Assign values to each Letter Or Number in JavaScript? | SoloLearn: Learn to code for FREE!


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 😁🙏

3/16/2018 12:29:12 PM

Raaja Thalapathy

5 Answers

New Answer


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()] + " "; }


np @Raaja 😎


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.


@Burey Wow never thought it would be this Easy! Thanks a lot 😁🙌🏻


@Jericho Arcelao Thanks bro, Think that would help 😁👌🏻