ChoreBot JavaScript -- Need Help

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

Hello,

I need some help with my ChoreBot JavaScript.

I am having difficulty with the Good luck button. My button does not ever change. I cannot win or lose the game. It is as though that part of my code is not connected to the rest of my code. My doors change when I click on them. So I really think it is something to do with the code for the Good luck button.

I watched the entire 58 minute video and could not see how my code is any different from hers, so I am really lost as to what I am doing wrong.

Any help would be greatly appreciated. Here is my JS code. Thank you.

// Global Variables

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;

// Functions

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

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

playDoor = (door) => {
numClosedDoor–;
if (numClosedDoor === 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;
openDoor1 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else { //choreDoor = 2
openDoor3 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
}

}

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

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

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

// To call a function from inside a click handler
startButton.onclick = () => {
if (!currentlyPlaying) {
startRound();
}
}

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

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

// Call the functions

startRound();

1 Like

May not be THE problem, but definitely A problem.

2 Likes

Your playDoor function is missing return. See if that helps.

2 Likes

That function does not need a return. The actual problem has been identified as being in that function, though.

2 Likes

Thanks for the idea. But that was not the problem.

2 Likes

Adding the return did not change anything. But thank you for the idea.

2 Likes

All I changed was these two lines of the playDoor function…

numClosedDoors--;

and

if (numClosedDoors === 0)

The code worked fine after that.

Apart from a few minor tweaks for purpose of testing global variables, I used your code in this repl…

https://repl.it/@mtf/choredoor

2 Likes

Thank you so much. That was it. I had forgotten the ‘s’ on the end of numClosedDoors.

Thank you.

3 Likes

Ok my bad. :wink: I remember having the same or similar problem but I succeeded to solve it at the end.

2 Likes