The last test for my calculator is failing even though, when performed automatically, it works perfectly? Why would this be?
There test that’s failing is this one:
Pressing an operator immediately following “=” should start a new calculation that operates on the result of the previous evaluation.
And this is the error message in its entirety:
14. Pressing an operator immediately following "=" should start a new calculation that operates on the result of the previous evaluation
Script error. (:0)
Error: Script error. (:0)
at r.onerror (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:575:14032)
at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:94595
at Array.forEach (<anonymous>)
at te (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:94541)
at n.<anonymous> (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:584:171917)
at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:569:259477
at ny.Dg.run (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:569:259772)
at r.Qg.runTest (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:569:274680)
at https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:569:275616
at o (https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js:569:274033)
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36
Challenge: Build a JavaScript Calculator
Link to the challenge:
Any help greatly appreciated, I’ve been stuck on this for a few days now. I keep googling the same things worded slightly differently to try find a post discussing this but no love. Send help!
it(`Pressing an operator immediately following "=" should
start a new calculation that operates on the result of the previous
evaluation`, function () {
clickButtonsById([_5, _min, _2, _eq, _div, _2, _eq]);
assert.strictEqual(
getInputValue(document.getElementById('display')),
'1.5',
'The sequence "5 - 2 = / 2 =" should produce an output of "1.5" '
);
clearDisplay();
clickButtonsById([_5, _plus, _5, _eq, _plus, _3, _eq]);
assert.strictEqual(
getInputValue(document.getElementById('display')),
'13',
'The sequence "5 + 3 = + 3 =" should produce an output of "13" '
);
});
The trace in the error makes it seem to me that it is not just failing for “not working well” but is failing on some JS level, perhaps what ilenia is saying.
If you were asking about what I mean by my “failing on some JS level” I mean that there is an error being thrown somewhere in your code or the test code.
Ah yes, thank you I can see that now. It seems to be throwing its hands up when I select an operation directly after selecting equals… I don’t really know what “invalid left-hand side in assignment” means though. Clicking on those links, they’re specifying the eval on line 61, and the onClick for the the operative key… Weird
In JS, that means you are trying to assign something to something that cannot receive an assignment. For example, in JS 4 = 2 would be an invalid assignment - you can’t assign 2 into 4. I assume something like that is going on.
Thanks for pointing me in the right direction both of you. Putting the explanation down here for indexing purposes. Previously my code for handling operative keys was as below:
The error in the browser console only occurred when an operative is selected after the equals is pressed. I also noticed that I didn’t have an explicit case in my if/else statement for a key input of equals. So, I changed the end of the functionKey function to the following:
Now when both my variables for holding sums (sumX and sumY) both have an assigned value andequals is pressed, now it assigns the new value to sumX and clearssumY. So, in summary, the problem was that equals needed to be handled slightly differently than other operations, and I wasn’t allowing that in the functionKey function.
Woosh, glad that’s over. Time to get into the CSS side of things and make it look pretty.