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

189
Views
usar .substring después de .replace está creando un error a veces, ¿cómo podría estructurar correctamente la subcadena para que siempre funcione?

Recibo un error con mi código a continuación. Estoy tratando de hacer una calculadora, y estoy usando cadenas como entrada y encasillando para resolver. La calculadora funcionaba bien, pero ahora estoy tratando de agregar un botón de respuesta para permitir tener ecuaciones basadas en la respuesta anterior. Tengo un problema con las subcadenas ahora, y en las ecuaciones donde ans viene primero como 'ans+88", obtengo NaN como resultado y 88 se convierte en indefinido. Pero cuando cambio los parámetros de la subcadena, lo estropea para otros tipos de ecuaciones como se describe en los comentarios a continuación.

enlace al repositorio

 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 answers
Answer question

0

La solución que dio @james resolvió el problema, la posición del signo + obviamente iba a cambiar después de que reemplacé ans, por lo que esa línea solo tenía que moverse después del reemplazo, error tonto, agradezco la ayuda.

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!