Front End Development Libraries Projects - Build a JavaScript Calculator

Tell us what’s happening:
Describe your issue in detail here.

Hi, I am unable to pass these two run test questions:
##In any order, I should be able to add, subtract, multiply and divide a chain of numbers of any length, and when I hit “=”, the correct result should be shown in the element with the id of “display”

If 2 or more operators are entered consecutively, the operation performed should be the last operator entered (excluding the negative (-) sign.

help pleaselink to calculator
Your code so far
link to calculator[build a calculator](link to calculator)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36

Challenge: Front End Development Libraries Projects - Build a JavaScript Calculator

Link to the challenge:

Well, this project introduces some more complex issues that you don’t seem to have addressed in your code. For one, it doesn’t seem you’ve implemented the " ." into your code, but maybe you just haven’t gotten there yet.

Regarding the problems you’ve mentioned, it doesn’t seem you’ve included the ability to enter a negative number. For instance, entering 5 * - 5 should have a result of -25… Five times negative 5. Instead you get 0 because the - overwrote the *.

Also, it doesn’t seem you have programmed a method for stringing multiple operations together… for instance, 5 * 5 - 2 should result in 23 (five times five minus 2) but instead you’re calculator only remembers the last two operators, resulting in 5-2=3.

Seems you’ve got some more problem solving to do. Hope that makes sense. If you have a specific question regarding how to do that let us know.

Hi kinome79 please help I manage to add ’ . ’ decimal key however the last 2 method for stringing multiple operations together… for instance, 5 * 5 - 2 should result in 23
formula v immediate 3+5*6-2/4 string, I am lost .
Any hint would be appreciated.
please see my linkJS calculator

Well, first I wanted to say maintaining the state of your calculator through HTML element datasets is intriguing… I never considered it before and usually just used Javascript global vars. I like it… not sure if its considered a good way to do things, but at first I wasn’t sure where you were storing all these variables, and then was like, wait… neat :slight_smile:

Other than that, I don’t want to nay-say your design because maybe I don’t completely understand what’s going on, but it seems you’re relying on a very fixed equation layout… firstnum, operator, secondnum = result. The problem is that doesn’t leave room for someone typing a more complex operation.

For instance, when the calculator first comes up I typed . 5 = . The result was a blank screen. I then typed +2 and got a NaN error. It’s because I didn’t stay within your number, operator, number template.

As the point of the project is to design an app to solve this problem, I can’t really tell you how to design it. There are many ways, a complex if/then web to handle all possible input combinations, an ongoing first-operator variable that continuously updates as more complex equations are entered (this would be similar to how a basic 7-seg calculator would do it), an equation string that remembers all inputs and then coughs up a solution after hitting equals, a mix between those, another solution I would have never thought of, etc. Just need to re-think about the problem with these new challenges in mind, and decide how you’d like to solve it. Thats the fun part :slight_smile:

Not sure if you’re familiar with the javascript eval function but it was a useful function for my design. Not sure if it would help in yours.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.