Back

Activity 4.6 — Trivia Game

divider

Introduction

Activity 4.6

Building a Game with Arrays

Topics

  • Using arrays to store game data
  • Traversal to control game flow
  • Tracking scores
  • Processing results

Why Games?

Games use the same programming concepts as serious applications — they just present them in a more fun way.

In this activity, you’ll build a complete game using only tools you already know.

Parallel Arrays

This trivia game uses parallel arrays:

  • questions[i] holds a question
  • answers[i] holds the correct answer

Both arrays must stay the same length.

Demo: Trivia Loop

// Processing pattern: count correct answers
let score = 0;
let i = 0;
while (i < questions.length) {
let response = prompt(questions[i]).toLowerCase();
if (response == answers[i]) {
console.log("Correct!");
score++;
} else {
console.log("Incorrect.");
}
i++;
}
console.log("Final score:", score);

What Makes This a Complete Program?

  • User input
  • Stored data
  • Decision-making
  • Final output

'F' → Fullscreen

Objectives

  • icon Store game data using arrays.
  • icon Use traversal to control program flow.
  • icon Track and process a game score.
  • icon Build a complete, interactive program.
divider

Activity Tasks

  • icon Create a new project named trivia-game.
  • icon Complete each task individually.

Task 1: Build the Trivia Game

  • icon Ask each trivia question using a loop.
  • icon Compare user answers to the correct answers.
  • icon Keep track of the player’s score.
Task 1 — Trivia Game
console.log("--- Demo 1: Trivia Game ---");
// Trivia questions and answers
let questions = [
"What planet is known as the Red Planet?",
"How many bits are in a byte?",
"What language are we programming in?"
];
let answers = [
"mars",
"8",
"javascript"
];
// Score tracker
let score = 0;
let i = 0;
// Ask each question
while (i < questions.length) {
let response = prompt(questions[i]).toLowerCase();
if (response == answers[i]) {
console.log("Correct!");
score++;
} else {
console.log("Incorrect. The correct answer was: " + answers[i]);
}
i++;
}
// Final result
console.log("You answered " + score + " out of " + questions.length + " correctly.");
prompt("Press enter to continue...");

Task 2: Analyze Game Results

  • icon Store multiple scores in an array.
  • icon Calculate the average trivia score.
  • icon Print a summary message.
Task 2 — Game Summary
console.log("--- Demo 2: Trivia Game Summary ---");
// Example score data
let scores = [2, 3, 1, 3, 2];
// Calculate class average score
let total = 0;
let i = 0;
while (i < scores.length) {
total += scores[i];
i++;
}
let average = total / scores.length;
console.log("Average trivia score:", average);
if (average == scores.length) {
console.log("Perfect average! Everyone nailed it!");
} else if (average >= 2) {
console.log("Nice job overall!");
} else {
console.log("Looks like we need more practice!");
}
prompt("Press enter to continue...");
divider

Sample Output

Sample Output
--- Demo 1: Trivia Game ---
What planet is known as the Red Planet? mars
Correct!
How many bits are in a byte? 8
Correct!
What language are we programming in? javascript
Correct!
You answered 3 out of 3 correctly.
Press enter to continue... [Enter]
--- Demo 2: Trivia Game Summary ---
Average trivia score: 2.2
Nice job overall!
Press enter to continue... [Enter]
divider

Reflection Questions

You may write your reflection answers as comments at the bottom of your code.

  1. How do arrays help organize game data?
  2. Why must the questions and answers arrays stay the same length?
  3. How is this program similar to a Create Performance Task?
divider

Submission

Submit your activity and reflection answers to the appropriate dropbox.

Activity Complete