[go: up one dir, main page]

0% found this document useful (0 votes)
4 views3 pages

Apcsp PC

This JavaScript code implements a quiz application where users can add questions and answers, take the quiz, and receive their results. It includes functionalities for adding questions, navigating through the quiz, grading the answers, and restarting the quiz. The application ensures that at least one question is added before starting the quiz and provides feedback based on the user's performance.

Uploaded by

Melanie Ma
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)
4 views3 pages

Apcsp PC

This JavaScript code implements a quiz application where users can add questions and answers, take the quiz, and receive their results. It includes functionalities for adding questions, navigating through the quiz, grading the answers, and restarting the quiz. The application ensures that at least one question is added before starting the quiz and provides feedback based on the user's performance.

Uploaded by

Melanie Ma
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/ 3

JavaScript

// define all variables needed


var questions = [];
var answers = [];
var index = 0;
var correctCount = 0;
var numQuestions = 0;
var userAnswers = [];

// store all inputs from the user to make the quiz


// clears all input boxes when a question is added
onEvent("addQuestionButton", "click", function() {
appendItem(questions, getText("questionInput"));
appendItem(answers, getText("answerInput"));
setText("questionInput", "");
setText("answerInput", "");
});

/* if the user //never// clicks the "Add Question" button,


(which means the length of the list stays at 0), the screen changes to
the "result" screen and prompts the user to start over and enter at
least one question
*/
/* if the user enters at least one question, the screen will change to
the "takeTheQuiz" screen
*/
/* then, numQuestions is set to the length of the list of the
questions(how many questions inputted),
and the first inputted question is given to the user in the quiz
*/
onEvent("makeQuizButton", "click", function() {
if (questions.length == 0) {
setText("text", "Please add at least one question before
starting the quiz.");
setScreen("result");
} else {
numQuestions = questions.length;
currentQuestionIndex = 0;
correctCount = 0;
userAnswers = [];
setScreen("takeTheQuiz");
setText("question", questions[currentQuestionIndex]);
setText("answer", "");
}
});

/* every time the user clicks the "answerQuestionButton", the


"currentQuestionIndex" is increased by one
*/
/* if the currentQuestionIndex is less than numQuestions (we have not
reached the end of the quiz), the next question is displayed and the
answer input is cleared
*/
/* if currentQuestionIndex is equal to numQuestions (the user has
answered all questions),
the quiz is graded (calls the "gradeQuiz" function)
*/
onEvent("answerQuestionButton", "click", function() {
appendItem(userAnswers, getText("answer"));
currentQuestionIndex++;

if (currentQuestionIndex < numQuestions) {


setText("question", questions[currentQuestionIndex]);
setText("answer", "");
} else {
gradeQuiz(userAnswers);
}
});

// returns the count of how many of the user's inputted answer on the
quiz matches up with the answer entered for the question
// answersList {string} the name of the list of the user's answers for
the quiz
// return {number} the amount of questions answered right on the quiz
function gradeQuiz(answersList) {
correctCount = 0;
for (var i = 0; i < answersList.length; i++) {
if (answersList[i].toLowerCase() === answers[i].toLowerCase()) {
correctCount++;
}
}
setText("text", "You got " + correctCount + " out of " +
numQuestions + " questions correct!");
setScreen("result");
}

/* when the user clicks "restart", all variables are reset,


and the screen changes back to "quizMakerHome" to allow them to
create/take a new quiz
*/
onEvent("restart", "click", function() {
questions = [];
answers = [];
currentQuestionIndex = 0;
correctCount = 0;
numQuestions = 0;
userAnswers = [];
setScreen("quizMakerHome");
setText("questionInput", "");
setText("answerInput", "");
});

You might also like