Need help on a math wordle project

Hi, I’m trying to create something similar to this web game called Mathler, https://www.mathler.com/

and here is mine on codepen.

Basically, the game is similar to Wordle, but it’s in math. The game gives you a random number and you have to figure out the correct calculation that equals to the random number. And the calculation has to be 6 inputs long, including numbers and symbols.

My approach to this project is to generate a random number and generate calculations with conditions (not completely finished), and once the calculations were generated, I want to calculate the calculations to make sure they equal to the number, if one calculation is matched, then we have the answer users need to figure out. However, I’m stuck on this approach, I will explain this later on.

There is an alternative solution, which is to generate random calculations with the correct conditions, once one calculation is generated and it’s valid, the code can stop. Then we calculate the calculation to get the output, and the output would be the “random” number given to users. I personally think this is a better approach but my friend said this method is not efficient.

Both solutions require the code to actually calculate, which is another problem that I don’t know how to solve, I thought about saving the numbers into an empty array, for example, calculation = 1+45-4, perhaps I could save the numbers into an array, so something like [1, 45, 4], then program the code to recognize the plus and minus symbols, and using switch and case to calculate the output of first value from the array and second value from the array, and so on. But how can I program the code to read double digit numbers as in forty-five rather than four-five and group the two together as one value?

I want to know which approach is more appropriate in this case, or if there’s another easier and more efficient way to do this? I do think generate all those calculations will take up a lot of time but this is the best I could do right now. Any help would be greatly appreciated!

@camperextraordinaire I was just about to post this exact question, and a few others, but you beat me to it.

What are the constraints on your game? I’m assuming the answer should be a whole number? Can it be a negative number? Can it be `0`? Is there an upper/lower limit on the size of a number in the expression? Or for the answer? Can numbers be repeated? Can operators be repeated? Is the expression strictly evaluated from left to right or do multiplication and division take precedence over addition and subtraction?

Hi Randell, thank you for replying!

Yes, there should be constraints on the min and max number, or at least I hope so.

I have set
`let min = 1; let max = 100; let number = Math.floor(Math.random() * (max - min + 1)) + min; `

which I think will ensure the random number is a whole number and in the range of 1 to 100.

Neat… seems nobodies mentioned the `eval(str)` function for the calculation. If you compress the input boxes into a string, `eval(str)` can perform the calculation of that string and give you an answer I believe. Works on long equations too, like “3 + 5 / 3 * 6”, I believe following order of operations, but its been a while since I’ve used it… and I feel like I remember it was rather strict about single spaces between each operator and the numbers.