display.value = parseFloat(data.currentValue) - parseFloat(display.value);
When I press 6 my data.current value would = 6 then once i press the subtraction sign my display.value = -5.
6-(-5) = 11
How can I stop assigning the negative sign to my display.value? I could add another subtraction sign-on display. value so it subtracts, but I don’t think that’s the best solution. How can I stop adding whatever operator I click on to the display.value?
const calculatorKeys = document.querySelector('.calculator-keys');
const keys = calculatorKeys.querySelectorAll('button');
let display = document.querySelector('.calculator-screen');
const equalSign = document.querySelector('.equal-sign');
let data = {
currentValue: 0, //whatever value is in display gets stored here
operator: ""
}
let total;
const operators = ["+", "-", "*", "/",];
keys.forEach(key =>{ // output
key.addEventListener('click', () =>{
if(operators.includes(key.value)){
// update our data object current value to be the current display
data.currentValue = display.value; // whatever value I click will be current value
// then change the display to be the "=" view or other operator etc
display.value = key.value;
data.operator = key.value
// save the operator to our data object to get ready to use for some math on the next display value
}else {
display.value = display.value + key.value;
}
});
})
equalSign.addEventListener('click', () =>{
function calculate(){
if(data.operator === "+"){
display.value = parseFloat(data.currentValue) + parseFloat(display.value);
}else if(data.operator === "-"){
display.value = parseFloat(data.currentValue) - parseFloat(display.value);
}else{
console.log('not working')
}
}
calculate();
});
Thank you!