I'm currently working on a Javascript calculator project for FreeCodeCamp and encountering an issue with the code. Whenever I input an operator like "-", "+", "x", etc., the numbers in the history section start repeating themselves. For example, if I enter "5", then "5" appears in the history area. Subsequently, when I input "+", the history displays "5+5+".
My objective is to make the calculator function similar to the one found on Mac computers. In other words, when I input "5" followed by "+", I want the "5" to remain in the display area while "5+" appears in the history section until the next number is pressed. It should then display the next number while the history shows "5+6". How can I resolve these issues?
You can view my codepen link here: https://codepen.io/kikibres/pen/MEQvqv
Code snippet:
function addOperator(keyitem){
addNumber(keyitem);
subMath += mainMath;
mainMath = keyitem;
};
******************************UPDATE********************************** I managed to solve the repetition issue in the history area. However, I'm struggling with the display problem. Currently, every time I click on a number like "7" and then an operator like "+", the number in the display area resets to "0". Furthermore, after the operator, if I click on another number like "3", it displays as "03" in the display area... Here is my updated code snippet:
function addOperator(keyitem){
if(mainMath == "0"){
return;
}
subMath += keyitem;
mainMath = "0";
};