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

197
Vistas
how I can write this condition in javascript(Reactjs)

i recently start a calculator project with react. I create the project and it works fine but i just wanna add a new feature to it. i wanna when i click on a operation symbol (like "+", "-", "*", "/") if i click one more time and the operation that exist before that be the same (for example 1234+) when i click on "+" or "-" my code don't add it to the code and replace it with the before operation and also do that to "/" and "*"

I also attach the repository link so you can see my calculator source code by yourself. the repository of project source code

about 4 years ago · Santiago Gelvez
2 Respuestas
Responde la pregunta

0

Please update handleClick function like below:

    const handleClick = (e) => {
        if(result.length && result.charAt(result.length-1) != e.target.name || result.length == 0)
        setResult(result.concat(e.target.name));   
}
about 4 years ago · Santiago Gelvez Denunciar

0

On your handleClick function simply check if the current character is an operator symbol (e.g: '+','-','*','/') and the last character in the current result string is also an operator symbol. If they are both operator symbols simply do the following:

const handleClick = (e) => {
  if (result.length > 0 && (result[result.length - 1] === '+' || result[result.length - 1] === '-' || result[result.length - 1] === '*' || result[result.length - 1] === '/') && (e.target.name === '+' || e.target.name === '-' || e.target.name === '*' || e.target.name === '/')) {
    const newRes = result.slice(0, result.length - 1).concat(e.target.name);
    setResult(newRes);
  } else {
    setResult(result.concat(e.target.name));
  }
}

about 4 years ago · Santiago Gelvez 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