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

147
Views
Incrementing score on React Rap Quiz Not working

I'm using the setState to increment the score of a quiz I made. I'm using the callback method so I don't mutate the data but it's only showing me a score of 2. I don't get what i'm doing wrong here.. I'm calling the calculateScore function whenever the form is submitted onSubmit={this.calculateScore}.


        if(this.state.question1 === '1996') {
            this.setState((curr)=>{
                return {score: curr.score++}
            })
        }

        if(this.state.question2 === 'NWA') {
            this.setState((curr)=>{
                return {score: curr.score++}
            })
        }

        if(this.state.question3 === 'bad-boy') {
            this.setState((curr)=>{
                return {score: curr.score++}
            })
        }

        if(this.state.question4 === 'big-l') {
            this.setState((curr)=>{
                return {score: curr.score++}
            })
        }

        if(this.state.question5 === 'snoop-dogg') {
            this.setState((curr)=>{
                return {score: curr.score++}
            })
        }

        console.log(this.state);
        console.log('calculating')
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You are using the postFix operator value++ as opposed to prefix ++value. Try with prefix operator and it should work fine,

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Increment

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!