Chore Door : currently playing variable

Hello everyone,

This is my first time posting a question on any forum.

I am trying to complete the Chore Door exercise without the use of the “Tasks” for an extra challenge and to get my brain thinking.

My confusion keeps popping up for the “currently playing” variable and its boolean value that I placed in bold below.

what is the “currently playing” variable referring to? When the start button is clicked or not clicked? or something else?

especially the last section is confusing me:

startButton.onclick = () => {
if (currentlyPlaying === false) {
startRound();
}
}

Thank you to anyone in advance!

Unaltered script.js below:


// Access HTML elements

let botDoorPath = ‘Group 14’;
let beachDoorPath = ‘Group 16’;
let spaceDoorPath = ‘Group 15’;
let closedDoorPath = ‘Group 3’;

let numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;
let currentlyPlaying = true;

// Define game logic to check doors, progress game, end game, and choose a random chore door

doorImage1.onclick = () => {
if (currentlyPlaying && isClicked(doorImage1)) {
doorImage1.src = openDoor1;
playDoor(doorImage1);
}
}

doorImage2.onclick = () => {
if (currentlyPlaying && isClicked(doorImage2)) {
doorImage2.src = openDoor2;
playDoor(doorImage2);
}
}

doorImage3.onclick = () => {
if (currentlyPlaying && isClicked(doorImage3)) {
doorImage3.src = openDoor3;
playDoor(doorImage3);
}
}

startButton.onclick = () => {
if (currentlyPlaying === false) {
startRound();
}
}

// Start a game round

Based on the code you pasted, here is my understanding:

  • When the page loads for the very first time, we can start clicking on the doors straight away without the need of clicking the Start button, because of the declaration and initialization: let currentlyPlaying = true;

  • Once we have started a new round, currentlyPlaying will be true. If in the middle of the round, we click the start button, nothing will happen. We won’t start a new round or change the status of currentlyPlaying. If we click a door, it should open if it is closed and if already open, then nothing happens.

  • When the round ends (either by winning or losing), then currentlyPlaying will be set to false. The && condition in the doorImage onclick event handlers will evaluate to false because currentlyPlaying is false. Therefore, if we click on the doors, nothing should happen. When we click the start button, then currentlyPlaying will be set to true again. A new round will begin with all the doors being reset and closed.

startButton.onclick = () => {
    if (currentlyPlaying === false) {
        startRound();
    }
}

This basically says that if the start button is clicked and we are in the middle of the round (i.e. currentlyPlaying is true), nothing happens. If the round has ended (in win or loss i.e. currentlyPlaying is false), then a new round will be started. (Within startRound(), currentlyPlaying will be set to true).