Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda