var quizQuestions = [

{question: "What is the capital of France?", answer: "Paris"},
{question: "What year did the Titanic sink?", answer: "1912"},
{question: "Who wrote 'Hamlet'?", answer: "Shakespeare"}
];
var correctAnswersCount = 0;
var totalQuestionsCount = quizQuestions.length;
onEvent("startQuizButton", "click", function( ) {
setScreen("screen2");
});
onEvent("backButtonQuizSelect", "click", function( ) {
setScreen("screen1");
});
onEvent("generalKnowledgeBtn", "click", function( ) {
setScreen("screen3");
});
onEvent("historyBtn", "click", function( ) {
setScreen("screen3");
});
onEvent("scienceBtn", "click", function( ) {
setScreen("screen3");
});
onEvent("popCultureBtn", "click", function( ) {
setScreen("screen3");
});
onEvent("backbtn", "click", function( ) {
setScreen("screen2");
});
onEvent("easyBtn", "click", function( ) {
setScreen("screen4");
});
onEvent("checkAnswer", "click", function( ) {
correctAnswersCount = correctAnswersCount + 100;
setNumber("scoreLabel", correctAnswersCount);
});
function calculateScore(correctAnswers, totalQuestions) {
var score = 0;
if (totalQuestions > 0) {
score = (correctAnswers / totalQuestions) * 100;
}
return score;
}
var userScore = calculateScore(correctAnswersCount, totalQuestionsCount);
setText("scoreLabel", "Your score: " + userScore + "%");

Ul controls
Data
Control
Variables
Canvas
Turtle
Math
Functions

1 answer

It looks like you're working on a quiz application with a JavaScript structure for handling user interactions and calculating scores. Let's enhance your code snippet a bit to make sure everything works well, and I'll offer some clarification on key aspects as well.

Updated Code Explanation

  1. Quiz Structure: You're using an array of objects to store quiz questions and answers.
  2. Button Interaction: You have buttons for navigating between screens and checking answers.
  3. Scoring Logic: You are updating the score when the user checks an answer and calculating the final score based on correct answers.

Suggested Enhancements

I will add logic to handle checking the actual answers, and some adjustments for a cleaner score computation.

var quizQuestions = [
  {question: "What is the capital of France?", answer: "Paris"},
  {question: "What year did the Titanic sink?", answer: "1912"},
  {question: "Who wrote 'Hamlet'?", answer: "Shakespeare"}
];

var correctAnswersCount = 0; // Number of correct answers
var currentQuestionIndex = 0; // Position of the current question
var totalQuestionsCount = quizQuestions.length; // Total number of questions

onEvent("startQuizButton", "click", function() {
  setScreen("screen2");
  displayQuestion(); // Show the first question
});

onEvent("backButtonQuizSelect", "click", function() {
  setScreen("screen1");
});

onEvent("generalKnowledgeBtn", "click", function() {
  setScreen("screen3");
});

onEvent("historyBtn", "click", function() {
  setScreen("screen3");
});

onEvent("scienceBtn", "click", function() {
  setScreen("screen3");
});

onEvent("popCultureBtn", "click", function() {
  setScreen("screen3");
});

onEvent("backbtn", "click", function() {
  setScreen("screen2");
});

onEvent("easyBtn", "click", function() {
  setScreen("screen4");
  correctAnswersCount = 0; // Reset score
  currentQuestionIndex = 0; // Reset current question index
  displayScore(); // Reset score display
  displayQuestion(); // Show first question
});

onEvent("checkAnswer", "click", function() {
  var userAnswer = getText("answerInput"); // Get user input
  if (userAnswer.trim().toLowerCase() === quizQuestions[currentQuestionIndex].answer.toLowerCase()) {
    correctAnswersCount++;
  }
  currentQuestionIndex++; // Move to the next question
  if (currentQuestionIndex < totalQuestionsCount) {
    displayQuestion(); // Display next question
  } else {
    // End of quiz
    var userScore = calculateScore(correctAnswersCount, totalQuestionsCount);
    setText("scoreLabel", "Your score: " + userScore + "%");
    setScreen("resultScreen"); // Redirect to results screen
  }
});

function displayQuestion() {
  var currentQuestion = quizQuestions[currentQuestionIndex].question;
  setText("questionLabel", currentQuestion); // Assuming there's a label to display the question
  setText("answerInput", ""); // Clear previous input
}

function calculateScore(correctAnswers, totalQuestions) {
  if (totalQuestions > 0) {
    return (correctAnswers / totalQuestions) * 100; // Calculate percentage score
  }
  return 0; // Avoid division by zero
}

function displayScore() {
  setText("scoreLabel", "Your score: 0%"); // Reset score display
}

Key Changes:

  • Dynamic Question Display: I've included a displayQuestion function that shows questions and accepts user answers.
  • Answer Checking: When checking an answer, it compares the user's response with the correct answer, accounting for case insensitivity.
  • Screen Handling: I've added a mechanism to show the results screen once the quiz is over.
  • Input Handling: Uses getText("answerInput") to capture user input from an input field.

Assumptions:

  • This code assumes you have UI elements such as questionLabel for displaying questions, answerInput for user input, and resultScreen for the final score display. You would need to adjust UI identifiers per your actual setup.

With these enhancements, your quiz application should function more robustly and provide an engaging user experience.