Advertisement
Guest User

Convert String To Numbers

a guest
Mar 26th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function stringToNumbers(str) {
  2.   //Basic function that matches specific letters with numbers saved as an object. Later it converts whole string
  3.     let matchesLetNum = {'a' : 0, 'b' : 1, 'c' : 2, 'd' : 3, 'e' : 4, 'f' : 5, 'g' : 6, 'h' : 7, 'i' : 8, 'j' : 9, 'k' : 10, 'l' : 11, 'm' : 12, 'n' : 13, 'o' : 14, 'p' : 15, 'q' : 16, 'r' : 17, 's' : 18, 't' : 19, 'u' : 20, 'v' : 21, 'w' : 22, 'x' : 23, 'y' : 24, 'z' : 25 }
  4.     let numbers = str.toLowerCase().split("");
  5.     //this is to make sure that whole string is splitted into array and in lower case.
  6.     return numbers.map(x => matchesLetNum[x]).join(" ");
  7. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement