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

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

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 Report

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 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!