Hello everyone,
I’ve finished the Chore Door project but I don’t know why my robot once it appears in the first door, it stays there forever. It only change the door when I refresh the page.
So here is my js code because the wrong thing can only be there. Please help!
let doorImage1 = document.getElementById(‘door1’);
let doorImage2 = document.getElementById(‘door2’);
let doorImage3 = document.getElementById(‘door3’);
const botDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg”;
const beachDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg”;
const spaceDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg”;
const closedDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg”;
let numClosedDoors = 3;
let currentlyPlaying = true;
let openDoor1;
let openDoor2;
let openDoor3;
let startButton = document.getElementById(‘start’);
const isBot = (door) => {
if (door.src === botDoorPath) {
return true;
} else {
return false;
}
};
const isClicked = (door) => {
if (door.src === closedDoorPath) {
return false;
} else {
return true;
}
};
const playDoor = (door) => {
numClosedDoors–;
if (numClosedDoors === 0) {
gameOver('win');
} else if (isBot(door)) {
gameOver();
}
};
const randomChoreDoorGenerator = () => {
const choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor2 = botDoorPath;
openDoor3 = beachDoorPath;
openDoor1 = spaceDoorPath;
} else if (choreDoor === 2) {
openDoor3 = botDoorPath;
openDoor2 = spaceDoorPath;
openDoor1 = beachDoorPath;
}
};
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) {
startRound();
}
}
const startRound = () => {
numClosedDoor = 3;
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
startButton.innerHTML = ‘Good luck!’;
currentlyPlaying = true;
randomChoreDoorGenerator();
}
const gameOver = (status) => {
if (status === ‘win’) {
startButton.innerHTML = 'You win! Play again?';
} else {
startButton.innerHTML = 'Game over! Play again?';
}
currentlyPlaying = false;
};
startRound();