Javascript Calculator Project

I’m having trouble trying to find a solution for this test

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

I’ve tried a few things but they result in messing up what I have now or doing nothing at all.

Any help would be appreciated

How are you trouble shooting this? The tests are telling you what expression is failing:

“The sequence “5 * - 5” = should produce an output of “-25” : expected ‘0’ to equal ‘-25’”

That’s telling you that when you type in 5 * - 5 your calculator is giving the result 0 instead of the expected result 25. Where do you think the issue is in your code?

Well I thought it had to do with the way it reads the " - " symbol, so I was trying to make a conditional that covers the situation where 2 operators are next to each other. Guess I’m not too sure how to properly put it in code.

Well you do not need to write a function to calculate written values instead could make code simpler taking input and using eval() inside js also will give you right answer for example, 5* -5 will be -25 with this code by using built in eval for reading expressions
example result box //
const result = document.getElementById(‘calcresultbox’);

const calculate = () => {
let resultSet = eval(result.value);
result .value = resultSet;

You can’t put it into code until you can put it into words. You need to figure out how you would solve this problem before you try to code it. Can you explain to us in plain language (no code) how you would handle multiple operators, including when the last one is a minus sign?

if two operators are pressed one after the other, the second one is a minus sign and it comes before a number press then the number should be recognized as a negative.

It works for the first number even if i press another operator before the minus sign.

You basically just restated one of the user stories, but you didn’t explain how you would solve it. What you have here is not really a coding issue. It’s a logic/implementation issue. I’m sure you have the ability to write the code, but you can’t do that until you know exactly why it isn’t working the way you want it to, and you have an idea of how to fix it. The fix could be very simple or it could require you to rewrite significant portions of your existing code. There is no way to tell until you at least understand why it’s happening.

The first questions I asked you are how are you troubleshooting this and where do you think the issue is in your code? I think you should start by pinpointing in your code where the issue is occurring. Once you do that then you should be able realize why it is happening. It took me just a few minutes to figure this out with some console.log statements. Then once you know the where and why you can start thinking about how to fix it.

So your next reply should hopefully be an explanation of why this is happening. You should be able to say something like “at this line here we should be doing this, but instead it is doing that, which is causing the wrong answer.”

I ended up finding an answer, I added a plus/minus button so if someone were to use the program they would just click on that instead. Doesn’t pass the test but there’s that.

if(firstValue &&
operator &&
previousKeyType === ‘operator’ &&
action === ‘subtracts’){
display.textContent = ‘-’ + displayedNum

I wrote this piece of code thinking it would work but it doesn’t add on the minus sign to the second number but the first.

I also noticed prior to writing that code snippet above 5 * - 5 is giving me zero, which means there being added together.

operator &&
secondValue &&
action === ‘subtracts’){
const calcValue = calculate(firstValue, operator, secondValue)
display.textContent = calcValue * -1
calculator.dataset.firstValue = calcValue

This technically passes the test but it treats the subtract sign like an equals sign so it can’t be right.

Well, it does allow you to do 5 * (-5) in your calculator but it does not pass the requirements for the project.

You are getting closer. Yes, when you click the equal sign after typing in 5 * - 5 it is actually calculating 5 - 5. The key here is to figure out why. You are keeping track of state with data attributes in the #holder div. I suggest you look at those to see what it going on.

It still feels like you don’t completely understand why this is happening and so you’ve created a workaround instead of fixing the actual problem. Get rid of that +/ button and concentrate on figuring out what is wrong with your logic. Only once you discover where the problem is can you then fix it.