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;
}