Hi, I am trying to add a quiz section to an educational portal that I am working on. This has multiple quizzes, each quiz has 20 questions. Only people who logged in can take the quiz, so I will be storing their user_id as well. There are different database collection Responses_ch* that store the responses, a different one named Quizzes that contain the right answers where each record is for a particular chapter and its fields contain the answers. After pressing the submit button, the response should be stored and the scores should be calculated. This is the code I have right now.
import wixData from 'wix-data';
import wixUsers from 'wix-users';
export function btnSubmit_click(event, $w) {
//Add your code for this event here:
let user = wixUsers.currentUser;
let toInsert = {
"title": user.id,
"ques_1": $w("#radioGroup1").value,
"ques_2": $w("#radioGroup2").value,
"ques_3": $w("#radioGroup3").value,
"ques_4": $w("#radioGroup4").value,
"ques_5": $w("#radioGroup5").value,
"ques_6": $w("#radioGroup6").value,
"ques_7": $w("#radioGroup7").value,
"ques_8": $w("#radioGroup8").value,
"ques_9": $w("#radioGroup9").value,
"ques_10": $w("#radioGroup10").value,
"ques_11": $w("#radioGroup11").value,
"ques_12": $w("#radioGroup12").value,
"ques_13": $w("#radioGroup13").value,
"ques_14": $w("#radioGroup14").value,
"ques_15": $w("#radioGroup15").value,
"ques_16": $w("#radioGroup16").value,
"ques_17": $w("#radioGroup17").value,
"ques_18": $w("#radioGroup18").value,
"ques_19": $w("#radioGroup19").value,
"ques_20": $w("#radioGroup20").value,
};
let options = {
"suppressAuth": true,
"suppressHooks": true
};
var scoreCount =0;
wixData.insert("Responses_ch1", toInsert)
.then( (results) => {
let item = results;
} )
.catch( (err) => {
let errorMsg = err;
} );
wixData.query("Quizzes")
.find()
.then( (results) => {
let chapter = results.items[0];
if ($w("#radioGroup1").value === chapter.items[0]){scoreCount +=1;}
if ($w("#radioGroup2").value === chapter.items[1]){scoreCount +=1;}
if ($w("#radioGroup3").value === chapter.items[2]){scoreCount +=1;}
if ($w("#radioGroup4").value === chapter.items[3]){scoreCount +=1;}
if ($w("#radioGroup5").value === chapter.items[4]){scoreCount +=1;}
if ($w("#radioGroup6").value === chapter.items[5]){scoreCount +=1;}
if ($w("#radioGroup7").value === chapter.items[6]){scoreCount +=1;}
if ($w("#radioGroup8").value === chapter.items[7]){scoreCount +=1;}
if ($w("#radioGroup9").value === chapter.items[8]){scoreCount +=1;}
if ($w("#radioGroup10").value === chapter.items[9]){scoreCount +=1;}
if ($w("#radioGroup11").value === chapter.items[10]){scoreCount +=1;}
if ($w("#radioGroup12").value === chapter.items[11]){scoreCount +=1;}
if ($w("#radioGroup13").value === chapter.items[12]){scoreCount +=1;}
if ($w("#radioGroup14").value === chapter.items[13]){scoreCount +=1;}
if ($w("#radioGroup15").value === chapter.items[14]){scoreCount +=1;}
if ($w("#radioGroup16").value === chapter.items[15]){scoreCount +=1;}
if ($w("#radioGroup17").value === chapter.items[16]){scoreCount +=1;}
if ($w("#radioGroup18").value === chapter.items[17]){scoreCount +=1;}
if ($w("#radioGroup19").value === chapter.items[18]){scoreCount +=1;}
if ($w("#radioGroup20").value === chapter.items[19]){scoreCount +=1;}
} )
.catch( (err) => {
let errorMsg = err;
} );
$w("#Score").text = scoreCount & "/20";
}
In theory this should work but neither the records get stored nor do i get the score count. Please help! I am not well versed with JavaScript. Thanks!
Hi,
Please share a link to your site so we can inspect ?
Roi