Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
JS calculator bug

P.S new to js, I made an ios like calculator in js in which the basic functioning is the user enters the first number which gets stored in a variable , screen gets cleared then the user enters the second number which again needs to be stored in a variable. I am currently having problem in storing the second number.


    const getnum = () => {
  let firstnum = parseFloat(ans.innerText);
  console.log("firstnum" + "=" + firstnum);
};

Copied this from internet to get the second number

operationperf = (operation) => {
  if (!numinmem) {
    numinmem = parseFloat(ans.textContent);
    operatorinmem = operation;
    ans.textContent = "";
    return;
  }
  operatorinmem = operation;
  ans.textContent = getResultOfOperationAsStr();
  ans.textContent = "";
};

but this causes both the variables to have the same value so if i do 50+78 the calculator performs 50+50

working demo: https://megahedron69.github.io/ios-Calculator/ javascript file: https://github.com/Megahedron69/ios-Calculator/blob/main/app.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First of all, it seems that your numinmem variable is not being used outside the scope of operationperf function for any further processing.

Also, in your following function:

const getResultOfOperationAsStr = () => {
  const currentValueNum = parseFloat(ans.textContent);
  const valueNumInMemory = parseFloat(ans.textContent);
  let newValueNum;
  if (operatorinmem === "addition") {
    newValueNum = valueNumInMemory + currentValueNum;
  } else if (operatorinmem === "subtraction") {
    newValueNum = valueNumInMemory - currentValueNum;
  } else if (operatorinmem === "multiplication") {
    newValueNum = valueNumInMemory * currentValueNum;
  } else if (operatorinmem === "division") {
    newValueNum = valueNumInMemory / currentValueNum;
  }
  return newValueNum.toString();
};

It appears that your are using the current value of the element stored in ans variable for both sides of your operations (e.g. ansValueAsFloat + ansValueAsFloat), hence, producing the undesired behavior.

My recommendation would be to set any of the variables involved in the final operation to the value of numinmem and set the other to the current value in ans.

Hope I made myself clear.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!