This is my current code
it works like
wordsToNum("one five eight twelve four") //outputs 15124
it returns numbers according to the words in string
I want it to work like this:-
wordsToNum("one hundred and twenty one") //outputs 121
with my code I have to write something like:- wordsToNum(“one two one”) which outputs “121”
const wordsTonum = (w) => {
const numbers = {
zero: 0,
one: 1,
two: 2,
three: 3,
four: 4,
five: 5,
six: 6,
seven: 7,
eight: 8,
nine: 9,
ten: 10,
eleven: 11,
twelve: 12,
thirteen: 13,
fourteen: 14,
fifteen: 15,
sixteen: 16,
seventeen: 17,
eighteen: 18,
nineteen: 19,
twenty: 20,
thirty: 30,
forty: 40,
fifty: 50,
sixty: 60,
seventy: 70,
eighty: 80,
ninety: 90,
};
let a = w.split(" ");
let numString = "";
for (let i = 0; i < a.length; i++) {
// console.log(a[i]);
for (let k in numbers) {
if (a[i] === k) {
numString += numbers[k].toString();
}
}
}
return Number(numString);
};
console.log(wordsTonum("one five eight twelve four"));
console.log(wordsTonum("one hundred and twenty one"));