Latest update:
My code on GitHub is updated. Here’s the repository link again: https://github.com/DragonOsman/javascript-calculator
My question: For the test about the display being cleared and showing only 0 when the button with id of “clear” is clicked on the FCC tests suite for the project, it says it expected “\n\n\n0 to equal 0” and I don’t know why mine is “\n\n\n0” at all; could someone help me out here?
For the test that says:
As I input numbers, I should be able to see my input in the element with the id of "display"
Mine fails with the error “Numbers do not display correctly within id=“display” : expected ‘\n\n\n3’ to equal ‘123’”. Even though when I click the numbers, I see them appear on the display as they should.
I have a div with with id of “display” with two nested paragraph elements, one with id of “stored” and the other with the id of “current” (the former is above the latter). When the user clicks on a number, it appears in the #current
element, and the formula being evaluated appears in the #stored
element. Numbers from the #current
element are appended to the #stored
element when an operator is clicked, with the clicked operator appearing after the number.
I have 7 out of 16 tests currently passing.
Related to that, I also have an issue where stored
is undefined
on line 274 even though I’ve defined it earlier in the code. In this function:
const handleOperatorClick = event => {
setOperatorClicked(true);
let stored;
// Have to set "/" and "*" characters for multiplication
// and division because with event.target.textContent values,
// expr-eval parser will error
// And if stored value is not an empty string, append the current value
// and the operator to it; otherwise, just set it to the current value
// with the operator next to it.
if (event.target.textContent === "+") {
if (storedValue.length > 0) {
stored = `${storedValue}${currentValue}${event.target.textContent}`;
} else if (storedValue.length === 0) {
stored = `${currentValue}${event.target.textContent}`;
}
} else if (event.target.textContent === "÷") {
if (storedValue.length > 0) {
stored = `${currentValue}${currentValue}/`;
} else if (storedValue.length === 0) {
stored = `${currentValue}/`;
}
} else if (event.target.textContent === "×") {
if (storedValue.length > 0) {
stored = `${storedValue}${currentValue}*`;
} else if (storedValue.length === 0) {
stored = `${currentValue}*`;
}
} else if (event.target.textContent === "-") {
if (storedValue.length > 0) {
stored = `${storedValue}${currentValue}${event.target.textContent}`;
} else if (storedValue.length === 0) {
stored = `${currentValue}${event.target.textContent}`;
}
}
if (reciprocalClicked) {
stored = `${storedValue}${event.target.textContent}`;
}
const prevStoredValue = stored.slice(0, stored.length - 1);
if (math.parse(prevStoredValue) !== null) {
const calculatedValue = math.evaluate(prevStoredValue);
setCurrentValue(`${calculatedValue}`);
}
setStoredValue(stored);
};
It’s undefined
on the line where I’m doing
const prevStoredValue = stored.slice(0, stored.length - 1);
Any help is appreciated. Thanks in advance. (And sorry for the multiple consecutive posts).
Edit: I managed to fix the problem with stored
becoming undefined there, but I also have a problem where after an operator or equals sign is clicked, all numbers clicked replace the one before it in the currentValue
string instead of getting appended to it, so I need to check if numbers were consecutively clicked after an operator or equals button was clicked. How do I do that?