0% found this document useful (0 votes)
21 views4 pages

CPT PRG Code

The document contains code for a math quiz application that displays multiple choice questions and tracks the user's score. It includes an array of questions and answers and event handlers to check responses, update scores and provide feedback.

Uploaded by

1254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views4 pages

CPT PRG Code

The document contains code for a math quiz application that displays multiple choice questions and tracks the user's score. It includes an array of questions and answers and event handlers to check responses, update scores and provide feedback.

Uploaded by

1254
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

4/17/23, 10:24 PM https://bakerfranke.github.

io/codePrint/

CPT PROGRAM CODE

1 var score = 0;
2
3 // array list containing questions and their corresponding answer
4 var mathQuestions = [
5 ({
6 "question": "6 x 6",
7 "correct": "36",
8 }),
9 ({
10 "question": "120 - 120",
11 "correct": "0",
12 }),
13 ({
14 "question": "100 + 68",
15 "correct": "168",
16 }),
17 ({
18 "question": "12 - 11",
19 "correct": "1",
20 }),
21 ({
22 "question": "4 + 4",
23 "correct": "8",
24 }),
25 ({
26 "question": "200 / 2",
27 "correct": "100",
28 }),
29 ];
30
31 //function for accessing the questions and their answers
32 function displayQA(){
33 setText("label1", mathQuestions[0].question);
34 setText("label2", mathQuestions[1].question);
35 setText("label3", mathQuestions[2].question);
36 setText("label4", mathQuestions[3].question);
37 setText("label5", mathQuestions[4].question);
38 setText("label6", mathQuestions[5].question);
39 }
40
41 displayQA();
42
43 //when right answer is chosen, green color shows with a sound effect and
44 //score updates
45 onEvent("text_input1", "change", function( ) {
46 var input = getProperty("text_input1", "text");
47 if(input == mathQuestions[0].correct){
48 setProperty("text_input1", "background-color", "green");
49 setProperty("text_input15", "hidden", true);
50 playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
51 score++;
52
https://bakerfranke.github.io/codePrint/ 1/4
4/17/23, 10:24 PM https://bakerfranke.github.io/codePrint/

53 setText("text_input2", "Score: "+score);


54 points(score);
55 checkAnswers(input);
56 }
57 else{
58 setProperty("text_input1", "background-color", "red");
59 setProperty("text_input15", "hidden", false);
60 setText("text_input2", "Score: "+score);
61 playSound("sound://category_music/crackle_loss_1.mp3", false);
62 }
63 });
64
65 onEvent("text_input3", "change", function( ) {
66 var input = getProperty("text_input3", "text");
67 if(input == mathQuestions[1].correct){
68 setProperty("text_input3", "background-color", "green");
69 setProperty("text_input14", "hidden", true);
70 playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
71 score++;
72 setText("text_input4", "Score: "+score);
73 points(score);
74 checkAnswers(input);
75 }
76 else{
77 setProperty("text_input3", "background-color", "red");
78 setProperty("text_input14", "hidden", false);
79 setText("text_input4", "Score: "+score);
80 playSound("sound://category_music/crackle_loss_1.mp3", false);
81 }
82 });
83
84 onEvent("text_input6", "change", function( ) {
85 var input = getProperty("text_input6", "text");
86 if(input == mathQuestions[2].correct){
87 setProperty("text_input6", "background-color", "green");
88 setProperty("text_input16", "hidden", true);
89 playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
90 score++;
91 setText("text_input7", "Score: "+score);
92 points(score);
93 checkAnswers(input);
94 }
95 else{
96 setProperty("text_input6", "background-color", "red");
97 setProperty("text_input16", "hidden", false);
98 setText("text_input7", "Score: "+score);
99 playSound("sound://category_music/crackle_loss_1.mp3", false);
100 }
101 });
102
103 onEvent("text_input8", "change", function( ) {
104 var input = getProperty("text_input8", "text");
105 if(input == mathQuestions[3].correct){
106 setProperty("text_input8", "background-color", "green");
107 setProperty("text_input17", "hidden", true);
108 playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
109
https://bakerfranke.github.io/codePrint/ 2/4
4/17/23, 10:24 PM https://bakerfranke.github.io/codePrint/
109
score++;
110
setText("text_input9", "Score: "+score);
111
points(score);
112
checkAnswers(input);
113
}
114
else{
115
setProperty("text_input8", "background-color", "red");
116
setProperty("text_input17", "hidden", false);
117
setText("text_input9", "Score: "+score);
118
playSound("sound://category_music/crackle_loss_1.mp3", false);
119
}
120
});
121
122
onEvent("text_input10", "change", function( ) {
123
var input = getProperty("text_input10", "text");
124
if(input == mathQuestions[4].correct){
125
setProperty("text_input10", "background-color", "green");
126
setProperty("text_input18", "hidden", true);
127
playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
128
score++;
129
setText("text_input11", "Score: "+score);
130
points(score);
131
checkAnswers(input);
132
}
133
else{
134
setProperty("text_input10", "background-color", "red");
135
setProperty("text_input18", "hidden", false);
136
setText("text_input11", "Score: "+score);
137
playSound("sound://category_music/crackle_loss_1.mp3", false);
138
}
139
});
140
141
onEvent("text_input12", "change", function( ) {
142
var input = getProperty("text_input12", "text");
143
if(input == mathQuestions[5].correct){
144
setProperty("text_input12", "background-color", "green");
145
setProperty("text_input19", "hidden", true);
146
playSound("sound://category_achievements/lighthearted_bonus_objective_2.mp3", false);
147
score++;
148
setText("text_input13", "Score: "+score);
149
points(score);
150
checkAnswers(input);
151
}
152
else{
153
setProperty("text_input12", "background-color", "red");
154
setProperty("text_input19", "hidden", false);
155
setText("text_input13", "Score: "+score);
156
playSound("sound://category_music/crackle_loss_1.mp3", false);
157
}
158
});
159
160
onEvent("startButton", "click", function( ) {
161
setScreen("screen2");
162
});
163
onEvent("button2", "click", function( ) {
164
setScreen("screen3");
165
166
https://bakerfranke.github.io/codePrint/ 3/4
4/17/23, 10:24 PM https://bakerfranke.github.io/codePrint/
166 });
167 onEvent("button4", "click", function( ) {
168 setScreen("screen4");
169
});
170
onEvent("button6", "click", function( ) {
171
setScreen("screen5");
172
});
173 onEvent("button8", "click", function( ) {
174 setScreen("screen6");
175 });
176 onEvent("button10", "click", function( ) {
177 setScreen("screen7");
178 });
179 onEvent("button12", "click", function( ) {
180
setScreen("screen8");
181
});
182
183
//function having 'score' parameter to determine the user's final score
184
function points(score) {
185 if (score>=6) {
186 console.log("Excellent!!");
187 setScreen("screen8");
188 setText("text_input20", score);
189 }
190 else if (score>0 && score>=5) {
191 console.log("You can do better!");
192
setText("text_input20", score);
193
} else {
194
console.log("You can do better!");
195
setText("text_input20", score);
196 }
197 }
198 //function to iterate through the mathQuestions list and then check
199 //whether the answer given by the user is equal to the right answer
200 function checkAnswers(userAnswer) {
201 for (var i = 0; i < mathQuestions.length; i++) {
202 var question = mathQuestions[i].question;
203
var correctAnswer = mathQuestions[i].correct;
204
if (userAnswer === correctAnswer) {
205
console.log(question + " You are correct!");
206
} else {
207 console.log(question + "Sorry, that's incorrect.");
208 }
}
}

PDF document made with CodePrint using Prism

https://bakerfranke.github.io/codePrint/ 4/4

You might also like