How can I write a function in JavaScript that takes two parameters (a number and a "string" for e.g. 4 and "two") returns the sum of both multiplied. if num = 4 and string = "three" then the function should return 12 as an int. I guess what I am asking is how do I get to convert the string number "two" to int 2.
            Asked
            
        
        
            Active
            
        
            Viewed 794 times
        
    -3
            
            
        - 
                    1There isn't an in-built function for turning number words into numbers. – evolutionxbox Aug 03 '21 at 14:24
 - 
                    Hi, So there is not native method to work with them, you have to manually run it with a switch statement, or put them in a array and take the index of the word and add 1 to it and then further proceed. – SARAN SURYA Aug 03 '21 at 14:25
 - 
                    [How do I ask and answer homework questions?](https://meta.stackoverflow.com/q/334822) – evolutionxbox Aug 03 '21 at 14:25
 - 
                    4Does this answer your question? [Javascript: Words to numbers](https://stackoverflow.com/questions/11980087/javascript-words-to-numbers) – Spectric Aug 03 '21 at 14:28
 
2 Answers
0
            
            
        If I understand you correctly if you npm install words-to-numbers you should be able to use that to convert your second parameter.
0
            
            
        something like this is the only way I can think of:
function convertStringToNumber(string) {
   if(string === 'one')
     return 1;
   if(string === 'two')
     return 2;
   if(string === 'three')
     return 3;
  /* keep going... */
}
        Rick
        
- 1,710
 - 8
 - 17