skip to Main Content

I am trying to convert text (E.g. Hello) to a number (E.g. 0805121215).
I have looked up many sources, but none of them have worked.

I have tried:
https://dev.to/sanchithasr/7-ways-to-convert-a-string-to-number-in-javascript-4l
and
thttps://www.geeksforgeeks.org/convert-a-string-to-an-integer-in-javascript/
I tried a few more sources but they all say that text outputs as NaN. I would like it to convert to a number, I don’t want Not a Number.

Here is an example of what I want to happen:
https://scratch.mit.edu/projects/948090655/

2

Answers


  1. Like this?

    const lettersToNumbers = (str: string) => {
      return str
        .toLowerCase()
        .split("")
        .map((char) => {
          const code = char.charCodeAt(0);
          if (code >= 97 && code <= 122) {
            return code - 96;
          }
          return char;
        })
        .join("");
    };
    
    console.log(lettersToNumbers("test")); //2051920
    
    Login or Signup to reply.
  2. first you need to define each letter to it’s number and each number to it’s letter
    then you would grab the letter from the inputted number or the number from the inputted letter

    the challange was reversing numbers back to letters in this case because the function will match each number to it’s corresponding letter, so i made each letter have 2 numbers and now when the functions reverse the number it will do that with 2 numbers at the time knowing that each 2 numbers is a letter.

    Example here

    https://jsfiddle.net/3m2hj1a7/1/

    function convertTextAndNumber(input) {
      const alphabetMapping = {
        A: '01', B: '02', C: '03', D: '04', E: '05', F: '06', G: '07', H: '08', I: '09', J: '10',
        K: '11', L: '12', M: '13', N: '14', O: '15', P: '16', Q: '17', R: '18', S: '19',
        T: '20', U: '21', V: '22', W: '23', X: '24', Y: '25', Z: '26'
      };
    
      const reverseAlphabetMapping = {
        '01': 'A', '02': 'B', '03': 'C', '04': 'D', '05': 'E', '06': 'F', '07': 'G', '08': 'H', '09': 'I', '10': 'J',
        '11': 'K', '12': 'L', '13': 'M', '14': 'N', '15': 'O', '16': 'P', '17': 'Q', '18': 'R', '19': 'S',
        '20': 'T', '21': 'U', '22': 'V', '23': 'W', '24': 'X', '25': 'Y', '26': 'Z'
      };
    
      function textToNumber(text) {
        const uppercaseText = text.toUpperCase();
        return Array.from(uppercaseText).map(letter => alphabetMapping[letter] || '').join('');
      }
    
      function numberToText(number) {
        const resultText = [];
        
        for (let i = 0; i < number.length; i += 2) {
          const letterCode = number.substr(i, 2);
          if (reverseAlphabetMapping[letterCode]) {
            resultText.push(reverseAlphabetMapping[letterCode]);
          }
        }
    
        return resultText.join('');
      }
    
      // Check if the input contains only numbers to determine the direction of conversion
      const isNumeric = /^d+$/.test(input);
    
      if (isNumeric) {
        return numberToText(input);
      } else {
        return textToNumber(input);
      }
    }
    
    // Example usage
    const inputText = 'HelloWorld';
    const inputNumber = '08051212152315181204';
    
    console.log(convertTextAndNumber(inputText));  // Output: '08051212152315181204'
    console.log(convertTextAndNumber(inputNumber)); // Output: 'HelloWorld'
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search