I am doing the Chore Door project in the Building Interactive Websites section. The program doesn’t work properly. When I click on a door, the image of the door disappears into the small icon indicating the image can’t be found. Also, the game doesn’t keep track of a win or a loss. If I click on the doors (even though I can’t see what is behind them, I always win.
I have gone through my code several times, I found a couple of typos, and I had a section of logic in the incorrect place. I have also looked at every hint and gone over the video and checked my code by line and even with my code being identical, it still doesn’t work. Lastly I made one correction I found in the forums removing brackets around an else statement. After all that, it still isn’t working. Can someone please look over my code and tell me where I am stumbling?
Thanks!
// Access HTML elements
let doorImage1 = document.getElementById('door1');
let doorImage2 = document.getElementById('door2');
let doorImage3 = document.getElementById('door3');
let startButton = document.getElementById('start');
let botDoorPath = 'https://content.codecademy.com/projects/chore-door/images/robot.svg';
let beachDoorPath = 'https://content.codecademy.com/projects/chore-door/images/beach.svg';
let spaceDoorPath = 'https://content.codecademy.com/projects/chore-door/images/space.svg';
let closedDoorPath = 'https://content.codecademy.com/projects/chore-door/images/closed_door.svg';
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
const isClicked = (door) => {
if (door.src === closedDoorPath){
return true;
} else return false;
}
const isBot = (door) => {
if (door.src === botDoorPath){
return true;
} else return false;
}
const gameOver = (status) => {
if(status === 'win'){
startButton.innerHTML = "You win! Play again?";
}else startButton.innerHTML = 'Game Over! Play again?';
currentlyPlaying = false;
}
const playDoor = (door) => {
numClosedDoors--;
if(numClosedDoors === 0){
gameOver('win');
} else if(isBot(door)){
gameOver();
}
}
const randomChoreDoorGenerator = ()=> {
let choreDoor = Math.floor(Math.random() * numClosedDoors);
if(choreDoor === 0){
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if(choreDoor === 1){
openDoor1 = beachDoorPath;
openDoor2 = botDoorPath;
openDoor3 = spaceDoorPath;
} else if(choreDoor === 2) {
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
openDoor3 = botDoorPath;
}
}
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
const startRound = () => {
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
numClosedDoors = 3;
currentlyPlaying = true;
startButton.innerHTML = 'Good Luck!';
randonmChoreDoorGenerator();
}
startRound();