Skip to content
Advertisement

All my operators work as an add operator in my calculator

So basically when I want to subtract, it adds numbers. When I wrote the same code for multiply and divide, it did the same thing. Instead of dividing, it was adding numbers etc. I have no idea why it’s happening. It’s like my javascript ignores dataset.action and treats operators like one button. If I put parseInt(num1) – parseInt(num2), then all operators subtract numbers.

JavaScript
JavaScript

Advertisement

Answer

It definitly needs to be cleaned up and probably should be remade where all operations/num presses are stored in a array to then be able to have more complex operations,.

The main idea here is to have a num1 and num2 variable that gets set to the first and second arguments of the operation, the operation type is stored in oper then when the user presses = is switch is triggered and does the operation

JavaScript
JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement