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

291
Visualizações
How to I make it so my calculator will calculate multiple digits?

When I calculate single digits like 4+3 or even 9*9, it does it just fine, but when I attempt to do multiple digits, it only calculates the first digit for each number. For example, 54+28 gives me 14. I'm not sure where the bug is, so here's my code except the stuff I know isn't important:



function reducer(state, {type, payload}){
 switch(type) {
   case ACTIONS.ADD_DIGIT:
     if(state.overwrite) {
       return {
         ...state,
         currentOperand: payload.digit,
         overwrite: false,
       }
     }
  

   
      
       return {
         ...state,
         previousOperand: evaluate(state),
         operation: payload.operation,
         currentOperand: null
       }

     case ACTIONS.EVALUATE:
       if(
         state.operation == null || 
         state.currentOperand == null || 
         state.previousOperand == null
         ) {
         return state
       }
       return {
         ...state,
         overwrite: true,
         previousOperand: null,
         operation:null,
         currentOperand: evaluate(state),
       }
 }
}

function evaluate({currentOperand, previousOperand, operation}){
 const prev = parseFloat(previousOperand)
 const current = parseFloat(currentOperand)
 if (isNaN(prev) || isNaN(current)) return ""
 let computation = ""
 switch(operation) {
   case "+":
     computation = prev + current
     break
   case "-":
     computation = prev - current
     break
   case "*":
     computation = prev * current
     break
   case "÷":
     computation = prev / current
     break
 }
 return computation.toString()
}

about 4 years ago · Juan Pablo Isaza
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