Calculator project functionality

Hello, I’m working on a calculator project but i’m having trouble in making sure it works properly, regarding the second number that i type in. basically, it allows multiple decimal points instead of just one. I also can’t seem to backspace on the operator either nor can i use my keyboard. For the decimal problem for my second number, I think i’ve isolated the problem to be on line 16 after i tried debugging it. May i ask if someone can help guide me in the right direction to fix these issues? Thanks!
link:https://repl.it/@RyanMabanta/calculator#script.js

Hi,
I think the problem you have is between lines in 43 and 46. num2, to me, it will accept multiple dots as input. Probabaly should be omitted.

okay, i omitted those lines that but it appears that my calculator won’t accept any input altogether for num2 since it stops at line 16 , so i can’t see if num2 now only accepts one decimal point

OK. I see. So you have this:

if(!op){ 
   //Here you do all  the logic to set num1 &  num2 and check for multi-dots
} else if(num2.length < 13) {
   /* This is where I see the problem.
      I guess here where you start taking input for num2
     and here where you have to check problem for multi dots.
     But still it won't work because you are using else if
   */
    num2 += input;
}

I think should as below.

if(!op){ 
  // Do the logic for num1
} else {
 // Do the logic for num2
}

Hey, thank you very much! That just fixed the decimal problem!

Though my calculator won’t backspace the full expression. Probably has something to do with my event listeners?

Just tried this and it works for me. I have changed the function “populateDisplay” a bit and it can be better improved, because there a repetition of the same logic for num1 and num2:

function populateDisplay(input) {
  if (/[0-9\.]/.test(input)) {
    //console.log('1. ', input);
    if (!op) {
      if (input === ".") {
        if (num1.includes(".")) {
          return;
        } else if (num1 == "0") {
          num1 = "0.";
        } else {
          num1 = num1.concat(".");
        }
      } else {
        if (num1 === "0" && input === "0") {
          num1 = "0";
        } else if (num1 === "0" && input !== "0") {
          num1 = input;
        } else {
          if (input.length < 13) {
            num1 += input;
          }
        }
      }
    } else {
      if (input === ".") {
        if (num2.includes(".")) {
          return;
        } else if (num2 == "0") {
          num2 = "0.";
        } else {
          num2 = num2.concat(".");
        }
      } else {
        if (num2 === "0" && input === "0") {
          num2 = "0";
        } else if (num2 === "0" && input !== "0") {
          num2 = input;
        } else {
          if (input.length < 13) {
            num2 += input;
          }
        }
      }
    }
  } else {
    console.log(`input is ${input}`); //called in console every time an operator is typed or clicked
  }
  //answer = num1 + op + num2;
  fullEquation.textContent = `${num1}${op}${num2}`;
  //mainDisplay.textContent  = `${answer}`;
  mainDisplay.textContent = `${num1}`;
  // mainDisplay.textContext  = operations;
}

Problem you have is on trimLast function and should go like this:

function trimLast() {
  if (op && num2 !== "") {
    num2 = num2.slice(0, -1);
  } else if (op && num2 === "") {
    op = "";
  } else {
    num1 = num1.slice(0, -1);
  }
  populateDisplay();
}

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