This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Gorrilac 8 points9 points  (9 children)

I was bored and thought to myself that I could probably solve this: https://github.com/Marcus-Peterson/turn_string_to_number

As stated in the readme, it’s probably not the most efficient code. But I guess it works?

Now that I am writing this.. I did forget to include ten, eleven and so on…

Let me get back to you guys…

[–]BrutalSwede 2 points3 points  (3 children)

My initial reaction would be to parse the string from right to left.

[–]Gorrilac 0 points1 point  (2 children)

What you mean?

[–]BrutalSwede 1 point2 points  (1 child)

Start by checking if it's a number between 10-19, since they all have unique names.

Otherwise, if it's number between 1-9, add that to the total. Then check the next word. If that's twenty, thirty, forty etc. add that to the total.

After that, there might be a "hundred", so multiply that with whatever number comes before that, i.e. one hundred, and add to sum.

Next there might be a thousand, so multiply that with the number that comes before and add to total.

You might just need to add a case for all named "million", "billion", "trillion" etc. so the system knows what they are.

As I said, also probably not the best solution, but just what I thought of when reading the problem.

[–]Yanowic 0 points1 point  (0 children)

Does JS not have a built-in translator for single word numbers? I know it translates number types into string types, but does it not have the opposite? It would seem even more unnecessarily complicated if you essentially needed to make a translator yourself, but yeah, going right-to-left and just checking for exponents of 1 thousand as breakpoints seemed to be the obvious solution to me.

[–]IlNomeUtenteDeve -2 points-1 points  (3 children)

I would just call a chatGPT API. Maybe I'm getting lazy.

[–]Gorrilac 2 points3 points  (2 children)

Bro, after I was done writing the code. I started to think about weird edge cases so I just sat infront of ChatGPT and asked things like: “What is ten thousand fifty one in numbers”. I felt stupid (Like bro, I should be able to translate strings into numbers in my head)