Chapter 15: Final Challenge – Wizarding Tournament Scoreboard
Wizarding Tournament Scoreboard
Welcome to the Wizarding Tournament! In this final challenge, we'll create a scoreboard system for a magical tournament. This project will incorporate arrays, loops, conditionals, and functions all together. Think of it like combining everything to simulate a competition.
🏆 Tournament Concept
Let's imagine a tournament with multiple challenges (tasks) and several contestants (which could be individuals or houses). For simplicity:
- We have 3 tasks in the tournament.
- We have 4 contestants (let's say the four Hogwarts houses competing, or four champions).
We will simulate each task where contestants earn points, and then calculate the total score and determine the winner at the end.
⚙️ Planning the Structure
We'll break it down:
- Represent contestants and their scores (initially 0 for all).
- For each task:
- Simulate or assign points to each contestant.
- Update their cumulative scores.
- Possibly print the standings after each task.
- After all tasks, determine the overall winner.
We'll use random generation for points to simulate performance in tasks, to keep it dynamic.
📋 Setting up Contestants
We'll use arrays for contestants and scores:
let contestants = [
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
];
let scores = [0, 0, 0, 0]; // parallel array to keep scores aligned with contestants
So scores[0] is Gryffindor's score, etc.
🎲 Simulating Tasks with Random Points
Assume each task gives each contestant a score between 0 and 100 (just an arbitrary range). We'll use Math.floor(Math.random() * 101) to get 0-100.
We can simulate each task in a loop:
let tasks = 3;
for (let t = 1; t <= tasks; t++) {
console.log(`\nTask ${t} results:`); // \n to add a blank line for readability
for (let i = 0; i < contestants.length; i++) {
let pointsEarned = Math.floor(Math.random() * 101); // 0 to 100
scores[i] += pointsEarned;
console.log(
contestants[i] +
" earned " +
pointsEarned +
" points in Task " +
t +
"."
);
}
}
This nested loop will iterate tasks 1 through 3, and for each task, iterate through each contestant to assign and add random points, printing what each earned.
📊 Final Scores and Determining the Winner
After the tasks loop, the scores array holds total points for each contestant. We should print those:
console.log("\nFinal Scores:");
for (let i = 0; i < contestants.length; i++) {
console.log(contestants[i] + ": " + scores[i] + " points");
}
Now, to find the winner, we find the largest score and track the index:
let highestScore = -1;
let winnerIndex = -1;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > highestScore) {
highestScore = scores[i];
winnerIndex = i;
}
}
let winnerName = contestants[winnerIndex];
console.log(
`🏅 The Winner of the Wizarding Tournament is ${winnerName} with ${highestScore} points!`
);
We initialized highestScore to -1 (since scores are 0 or above, starting at -1 ensures any score will replace it). As we loop, whenever we find a new high score, we update highestScore and note the winnerIndex. After the loop, winnerIndex corresponds to the champion.
This approach assumes a single winner (no tie). If there's a tie for top score, this will pick the first one that achieved that score. Handling ties would require checking if another contestant equals highestScore and then declaring a tie or comparing further, but we'll keep it simple.
🏃 Running the Tournament
Let's put it all together in code form:
let contestants = [
"Gryffindor",
"Hufflepuff",
"Ravenclaw",
"Slytherin",
];
let scores = [0, 0, 0, 0];
let tasks = 3;
console.log("Starting Wizarding Tournament...");
for (let t = 1; t <= tasks; t++) {
console.log(`\nTask ${t} results:`);
for (let i = 0; i < contestants.length; i++) {
let pointsEarned = Math.floor(Math.random() * 101);
scores[i] += pointsEarned;
console.log(
contestants[i] +
" earned " +
pointsEarned +
" points in Task " +
t +
"."
);
}
}
console.log("\nFinal Scores:");
for (let i = 0; i < contestants.length; i++) {
console.log(contestants[i] + ": " + scores[i] + " points");
}
let highestScore = -1;
let winnerIndex = -1;
for (let i = 0; i < scores.length; i++) {
if (scores[i] > highestScore) {
highestScore = scores[i];
winnerIndex = i;
}
}
let winnerName = contestants[winnerIndex];
console.log(
`🏅 The Winner of the Wizarding Tournament is ${winnerName} with ${highestScore} points!`
);
When you run this, you'll get output something like:
Starting Wizarding Tournament...
Task 1 results:
Gryffindor earned 42 points in Task 1.
Hufflepuff earned 77 points in Task 1.
Ravenclaw earned 5 points in Task 1.
Slytherin earned 90 points in Task 1.
Task 2 results:
Gryffindor earned 13 points in Task 2.
Hufflepuff earned 88 points in Task 2.
Ravenclaw earned 20 points in Task 2.
Slytherin earned 55 points in Task 2.
Task 3 results:
Gryffindor earned 100 points in Task 3.
Hufflepuff earned 64 points in Task 3.
Ravenclaw earned 72 points in Task 3.
Slytherin earned 9 points in Task 3.
Final Scores:
Gryffindor: 155 points
Hufflepuff: 229 points
Ravenclaw: 97 points
Slytherin: 154 points
🏅 The Winner of the Wizarding Tournament is Hufflepuff with 229 points!
Your numbers will vary each run due to randomness, and thus the winner might differ each time.
🔍 Things to Notice
- We used nested loops: one for tasks, one for contestants inside that.
- We updated an array of scores using indices.
- We used template literal for the winner announcement (the string with
${}), which is a convenient ES6 feature for string building. - We included an emoji for the winner line to make it celebratory.
🌟 Expand the Challenge
- Increase the number of tasks or contestants. Perhaps ask the user for those numbers using
prompt. - Instead of random points, maybe have preset points for each task stored in a 2D array. But that might be overkill here.
- Include some special bonus, e.g., one random task gives double points to simulate a "golden snitch" or something.
- Output a ranking of all contestants, not just the winner (sorting could be used, but that's a bit advanced for now).
By completing this final challenge, you've demonstrated the ability to handle a complex problem with code. You used data structures (arrays), control structures (loops and conditionals), functions, and randomness to create a functional program. Bravo!