Calculator Project - issues with equal button

Hi there: I was able to make the equal button work. I am currently trying to make the signs (+, -, *, /) act like a equal button, so if I press 1 then + then 1 then +, I get 2. I was able to do that too with, currently, addition and subtraction. However, at this point, the equal button is not properly working: it only adds, but it doesn’t subtract. I thought it was (it still might be) something to do with the conditionals in the equal button, but it seems that it’s something to do with the logic–i.e. how the `if` statements are arranged.

I’m going to post snippets of my code and and the link to my Github for the full code.

``````function addButton() {
symbol = '+'
if(a !== '' && symbol === '+') { // a is not empty, thus it is pushed into c. Then a is cleared and symbol is +
c.push(Number(a));
a = '';

console.log(symbol);

if(a === '' && symbol === '+') { // when equals clears a, a symbol is chosen so it can do a process with the "number on the screen"
totalAmount = c.reduce((total, amount) => total + amount);
clearArr();
c.push(totalAmount);
console.log(totalAmount);
console.log(symbol);
``````
``````function subtractButton() {
symbol = '-'
if(a !== '' && symbol === '-') { // a is not empty, thus it is pushed into c. Then a is cleared and symbol is -
c.push(Number(a));
a = '';
console.log(symbolSub);
console.log(symbol);

};

if(a === '' && symbol === '-') { // when equals clears a, a symbol is chosen so it can do a process with the "number on the screen"
console.log(symbol);
console.log(a);
totalAmount = c.reduce((total, amount) => total - amount);
clearArr();
c.push(totalAmount);
console.log(totalAmount);
console.log(symbol);
``````
``````function equalsButton() {
//console.log(symbol);
if((symbol !== '-' && a !== '' )) {
c.push(Number(a));
console.log(symbol);
totalAmount = c.reduce((total, amount) => total + amount);
clearArr();
c.push(totalAmount);
console.log(totalAmount);
}
if((symbol === '-' && a !== '')) {
c.push(Number(a));
console.log(symbol);
totalAmount = c.reduce((total, amount) => total - amount);
clearArr();
c.push(totalAmount);
console.log(totalAmount);
};
``````

Is the issue here with how I’ve set up the conditions to be met? Is it the logic or something else? Thanks for the help.

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