Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

110
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda