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

138
Views
Function not returning the value inside variable after AJAX call

How do I get my function saveAttempt() to return score? The alert() displays the correct number in variable score, but the returned value is [Object Object]. Using toString(score) produces [object undefined].

What am I doing wrong?

function saveAttempt(uid,cid,code,success) {
    var score = $(document).ready(function(){   
                $.ajax({
                    type: "POST",
                    url: {$js_url} + '/wp-content/plugins/CSUKCoderTestEnviro/save_attempt.php',
                    data: {"uid" : uid, "cid" : cid, "code" : code, "success" : success},
                    success: function(data){
                        grabResponse = data;
                        grabResponse = JSON.parse(grabResponse);
                        let corr_attempt = 0;
                        let incorr_attempt = 0;
                        for(let i = 0; i < grabResponse.length; i++) {
                            if (grabResponse[i] == 0) {
                                incorr_attempt = incorr_attempt + 1;
                            } else {
                                corr_attempt = 1;
                            }
                        }
                        if (corr_attempt == 1) {
                            var score = 100 - incorr_attempt;
                        } else {
                            var score = 0;
                        }
                        alert(score);
                        }
                        });
                return score;
                }); 
            return score;
            }
about 4 years ago · Juan Pablo Isaza
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!