-3

I have a task to return number of a letter in alphabet instead of given letters. So, if I have the word 'Wow!' - I should return '23 15 23'.

the problem is I can't put spaces between numbers and have result '231523'.

When I try to put it I have an error if it's only a single letter - for 'a' I have '1 ' instead of '1'. How can I put it?

  • 2
    Can you share your current code? – roberrrt-s Nov 15 '16 at 09:42
  • you have to provide code without we cannot change in output – Mahi Nov 15 '16 at 09:43
  • 1
    Welcome to StackOverflow it would be worth reading http://stackoverflow.com/help/how-to-ask to get a feel for the information we need to answer a question well. As already commented the code, and the error message is a good place to start. – Jamiec Nov 15 '16 at 09:44
  • you would need to replace every letter with the corresponding numbers, if there are more letters you should add an space. also provide an minimal code to reproduce your problem. – Kevin Kloet Nov 15 '16 at 09:46

1 Answers1

3

You can try something like this:

  1. Split the string.
  2. Get the char code and do required operations
  3. Join the array with spaces.

var str = "Wow";
var opt = str.split("").map(function(el){ return el.toLowerCase().charCodeAt(0) - 96;}).join(" ");
alert(opt);
void
  • 36,090
  • 8
  • 62
  • 107