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

192
Visualizações
using .substring after .replace is creating an error sometimes, how could I properly structure the substring so that it always works?

I'm getting an error with my code below. I am trying to make a calculator, and I am using strings as input and typecasting in order to solve. The calculator was working fine, but now I am trying to add an ans button to allow for having equations based on previous answer. I am having an issue with the substrings now, and in equations where ans comes first like 'ans+88" I get NaN as a result and 88 turns into undefined. But when I switch the substring parameters it messes it up for other types of equations as described in the comments below.

link to repository

else if(equation.indexOf("+") != -1){
//this finds the the index of +
                let pInd = equation.indexOf("+")
               
        /*before I added this, it worked properly, firstValue would get the firstvalue
        secondvalue would get the second value and it added correctly
        the point of the if statement is to replace ans with props.prevResult which is 
       the previous answer from the previous entered equation. After it replaces it 
       removes the whitespace*/
                if(equation.includes("ᴀɴs")){
                   equation =  equation.replace(/ᴀɴs/gi, String(props.prevResult))
                  //my attempt to remove whitespace since i thought it might be the issue
                   // but now I'm pretty sure the issue is with substring.
                   equation = equation.replace(/\s/g, '')
                }
                equation = String(equation)
                firstVal = equation.substring(0,pInd)
                //this line is where the error seems to come from
                secondVal = equation.substring(pInd+1,equation.length)
                /* when ans is first such as in 'ans + 9" 9 becomes undefined ans stays 
                correct. I tried replacing it with pInd+1, equation.length-1, but that 
               made it so that equations like '9+ans' were cut short, 
               and regular ('1+55') equations also broke.*/

                result = parseFloat(firstVal)+parseFloat(secondVal) 
                props.setInput(result)
                props.setPrevResult(result)
                setResultGot(true)
            }
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The solution @james gave solved the issue, the position of the + sign was obviously going to change after I replaced ans, so that line just had to be moved to after the replacement, silly mistake, I appreciate the help.

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