I’m making a basic version of the game “Who Wants to be a Millionaire?” using Svelte. I think I have the setup correct thanks to another user on here, but I’m currently having difficulty with a specific aspect.
The flow of the program should go like this:
Click the start button & the content of the button changes to a question.
The answer fields below the question get populated with possible answers.
There’s a predetermined answer to each, when the right answer is clicked, it moves to the next question in the sequence.
If the wrong question is picked, the game simply ends with the start button content changed to “GAME OVER.”
Below is a copy of my code thus far, I’ve done my best to do a writeup:
<script>
//Initial definition of all my data.
let data = {
question: "Start Game",
options: ["Option 1", "Option 2", "option 3", "option 4"],
};
let selectedOption = "";
//Function that ends the game.
function endGame() {
data.question = "GAME OVER";
}
//Question functions: Each populates the data array with the question and answers.
function question1() {
data.question = "What game is this?";
data.options = ["Deal or No Deal", "Chess", "Who Wants to Be a Millionaire?", "Halo 3",
];
// MY ATTEMPT TO PROGRESS THE FIRST QUESTION TO THE NEXT.
if (document.querySelector("#ans_3").click) {
question2;
} else {
endGame;
}
}
function question2() {
data.question = "What is the answer to this question?";
data.options = ["Not this one", "This one", "Not this one", "Not this one"];
if (document.querySelector("#ans_2").click) {
}
}
</script>
<!-- HTML starts here. (In svelte) -->
<title>Who Wants to Be a Millionaire?</title>
<body>
<div class="container">
<h2 class="title">Who Wants to Be a Millionaire?</h2>
<p>
Click the start game button to begin. The game ends when you get a
question wrong.
</p>
<p>You can see your winning on the board to the right.</p>
<div class="game-div">
<div class="question-container">
<!-- Start button doubles as the question field. Starts the first question onclick. -->
<button class="startButton" on:click={question1}>{data.question}</button>
<!-- Dynamic options objects created below. -->
<div class="option-container">
{#each data.options as option, idx}
<label>
<input
id={"ans_" + idx}
type="radio"
bind:group={selectedOption}
name="scoops"
value={option}
/>
{option}
</label>
{/each}
</div>
</div>
</div>
</div>
</body>
I’m primarily trying to make it so answering the first question actually initiates the second one. I’m not quite sure how to do this. I’ve marked my attempt above.