Here is my logic: I add all the buttons’ values (numbers, operands etc.) to an array and when the equal button is clicked, I get the index of the operand, get all the numbers to that index = num1 , and get all the numbers from index to equal operand= num2, and call the operate function . I realize this won’t help me with the type of calculations like 4+6/3-2 but I can’t find the right way. Can you point me to the right direction? Here is the code: https://codepen.io/krykck/pen/PoqGZgY
Looking at the code, (I may be wrong) what it seems like is that you are missing a large understanding of javascript as well as html.
This is not a problem, but I suggest starting out small and make simpler projects before going into creating a calculator. And learn each concept step by step, and once you get good grasp of it, you will be able to find a solution fairly easily.
At that point you can pose the question again.
But, just to satisfy you, if you want to just want to create a calculator there is an eval operator. Refer this tutorial:
But I believe you want to try creating the logic in javascript, refer this tutorial:
Hope this helps!
Thank you for the feedback! I am doing The Odin Project and this is the next project. They do not allow us to use eval operator for this one. I knew my logic had problems and taught this way I could correct them . But maybe I should follow a tutorial.
Wait, before you follow a tutorial. Can you share the link of the project specification?
I may be able to help.
Because, I don’t want you to get stuck in tutorial hell.
I wasn’t sure of the context of your question, hence I directed you to a tutorial.
K, looking at it.
also, see this line in the specs:
This is the hardest part of the project. You need to figure out how to store all the values and call the operate function with them. Don’t feel bad if it takes you a while to figure out the logic.
So don’t worry if you don’t get it the first time, I estimate most people would take a couple of days to figure this out as a beginner.
I just taught I am completely stuck but you are right. I should spend more time to figure this out.
I am offering you this, try to figure out the solution in the following manner. But if you are not able to get it, come back and reply I may be able to help you.
Looking at this, here is what I am thinking:
- Write Pseodo code on how you may be able to approach this, it might look something like this:
(Your pseodo code will probably be different, I havent given much thought into my example
Go through string.
Group together values according to bodmas (or pedmas, whatever you are familiar with)
Operate them in order.
Output the value.
-
Break down the Pseudo code into smaller blocks that will be simple to implement
-
Then write each block in javascript, one bit at a time.
Then you can abstract them into functions like add, sub, etc…
Again reply back to this post if you are not able to figure it out, and if not me someone else can probably help you out.