someone can help me with some JS basics?
im a rookie, trying to do a calculator, that get the value in the screen, stores it in a variable called previousValue, then the calculator display sets clean and you digit the new number and then when you click equals, its stored in a currentValue variable, and then the operation with previous and current values occours and appear into the display. The issue is i dont know how to set the operator dynamicly to the equals function, that should works by getting the previousValue and operating with the currentValue, whataver the operator is And if i try to change things and do a switch case for each operatorButton.textContent that does the operation there storing in a variable, then when equals() is called just prints this variable on the display.
Things get worst to me, cause i also cant figure out a way to place functions inside de swicth case that calls
get PreviousNumber (a function that gets the number seted on the display and clean it to you digit a new number) and then WAITS till i digit a new number to execute currentNumber function (just gets the number seted in the screen if operator != null)
I dont know if i made things clear, if i didnt, sorry and let me know in the awnsers
here it is the code i have made so far (https://github.com/DanielRKlein/calculator-js)
i’ve explained above
2
Answers
i understand u used switch to be able to carryout the math operation but if you can merge your the textContent of
#previousOperationText
and#currentOperationText
and pass it as an argument to the JS built in eval function;Example
You can use JS object to store data and then use it after with new data and selected operator. Below is draft example of simple calc. Of course you need to write some symbol checks etc, but to get general idea it’s pretty ok. Check inline comments and check demo by clicking
Run code snippet
: