Chore Door! No Game Over


#1

https://www.codecademy.com/paths/web-development/tracks/build-interactive-websites/modules/web-dev-interactive-websites/projects/chore-door

Hey guys! I’ve been throught this exercise twice. Was able to correct some mistakes, but still there’s more.

Here is my code:

let doorImage1 = document.getElementById(‘door1’);
let doorImage2 = document.getElementById(‘door2’);
let doorImage3 = document.getElementById(‘door3’);
let startButton = document.getElementById(‘start’);
let botDoorPath = ‘https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg’;
let beachDoorPath = ‘https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg’;
let spaceDoorPath = ‘https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg’;
let closedDoorPath = ‘https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg’;
let numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;
let currentlyPlaying = true;

const isClicked = (door) => {
if (door.src === closedDoorPath) {
return false;
} else {
return true;
}
}

const isBot = (door) => {
if (door.scr === botDoorPath) {
return true;
} else {
return false;
}
}

const playDoor = (door) => {
numClosedDoors–;
if (numClosedDoors === 0) {
gameOver(‘win’);
} else if (isBot(door)) {
gameOver(‘lose’);
}
}

const randomChoreDoorGenerator = () => {
const choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor2 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else {
openDoor3 = botDoorPath;
openDoor1 = spaceDoorPath;
openDoor2 = beachDoorPath;
}
}

//Why using door1 instead of doorImage1?
door1.onclick = (door1) => {
if (currentlyPlaying && !isClicked(doorImage1)) {
doorImage1.src = openDoor1;
playDoor(door1);
}
}
door2.onclick = (door2) => {
if (currentlyPlaying && !isClicked(doorImage2)) {
doorImage2.src = openDoor2;
playDoor(door2);
}
}
door3.onclick = (door3) => {
if (currentlyPlaying && !isClicked(doorImage3)) {
doorImage3.src = openDoor3;
playDoor(door3);
}
}
startButton.onclick = () => {
if (!currentlyPlaying) {
startRound();
}
}

const startRound = () => {
door1.src = closedDoorPath;
door2.src = closedDoorPath;
door3.src = closedDoorPath;
numClosedDoors = 3;
currentlyPlaying = true;
startButton.innerHTML = ‘Good luck!’
randomChoreDoorGenerator();
}

const gameOver = (status) => {
if (status === ‘win’) {
startButton.innerHTML = ‘You win! Play again?’;
} else {
startButton.innerHTML = ‘Game over! Play again?’;
}
currentlyPlaying = false;
}

startRound();

Basically there’s no Game Over condition. If the first or the second door is the bot, the button doesn’t change.
Thanks!


#2

That question has surfaced many times, and it seems the course author does not wish to present an explanation, which leaves us all in the dark.

Change those variables and see what happens.


#3

Can you help me with the Game Over condition? It’s not working. Thanks!


#4

Could it have to do with ordering of the functions? What happens if all the listeners are written lowest in the code, just before startGame?