Chore Door function does not work

Hello, I am trying to figure out why this code does not work.
Maybe with the random function. But I can’t figure it out.

Chore Door Project

let doorImage1 = document.getElementById(“door1”);
let doorImage2 = document.getElementById(“door2”);
let doorImage3 = document.getElementById(“door3”);
let numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;

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

}
}

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’;

doorImage1.onclick = () => {
doorImage1.src = openDoor1;

}

doorImage2.onclick = () => {
doorImage2.src = openDoor2;
}

doorImage3.onclick = () => {
doorImage3.src = openDoor3;
}

randomChoreDoorGenerator();

OK. This is very stupit… When you declare a “else” you can’t add a condition…Since it is the default…

Hi, this might be the syntax you’re looking for:

if (condition1) {
...
} else if (condition2) {
...
} else { //<- no condition here so it only gets triggered when both of the previous conditions fail
...
}

Thank you!!!
Now it got more advanced and again my code does not working…

Have you any idea why?

let doorImage1 = document.getElementById(“door1”);
let doorImage2 = document.getElementById(“door2”);
let doorImage3 = document.getElementById(“door3”);
let numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;

const startButton = document.getElementById(‘start’);
const closedDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg”;

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

const playDoor = () => {
numCloseDoors --;
if(numCloseDoor === 0) {
gameOver(‘win’);
}

}

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

}
}

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’;

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

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

}

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

}

randomChoreDoorGenerator();

function gameOver(status) {
if(status === ‘win’) {
startButton.innerHTML = ‘You win! Play again?’;
}
}

Be sure your variable name is consistent throughout.

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';

Error: ‘botDoorPath’ was used before it was declared, which is illegal for ‘let’ variables.

Try declaring these variables before the function that uses them.

Thank you all. But it still does not work. The innerHTML does not update to the new text after opening all the doors.
I suspect that the problem is in action 53, but I am not sure…
Now that you’ve written the isClicked() function, put it to use.

Navigate to the three door element .onclick() functions and within each function, wrap the current logic within an if statement to determine if the isClicked() function has not yet happened for that particular doorImage .

Adding this logic now protects your game from shortcut victories by making each closed door clickable only once. Open the hint for an example of this functionality.

or action 56 Now that you have the startButton variable, let’s expand the gameOver() function.

Add status as a function parameter and write an if statement where the condition checks if status is equivalent to 'win' . If this condition equates to true, then the innerHTML of the startButton should change to You win! Play again? .

Who can help me???
Thanks:))

//connection doors to html
const doorImage1 = document.getElementById(‘door1’);
const doorImage2 = document.getElementById(‘door2’);
const doorImage3 = document.getElementById(‘door3’);
//images behind doors
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”;
//number of doors
const numClosedDoors = 3;
//doors open
let openDoor1;
let openDoor2;
let openDoor3;

//door closed
const closedDoorPath = “https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg”;

//start button
let startButton = document.getElementById(‘start’);

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

//check is game winning condition
const playDoor = () => {
numClosedDoors–;
if (numClosedDoors === 0) {
gameOver(‘win’);
}
}

//randomise the doors
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 = beachDoorPath;
openDoor2 = spaceDoorPath;
}
}

//when clicked you get new doors
doorImage1.onclick = () => {
if(!isClicked(doorImage1)){
doorImage1.src = openDoor1;
playDoor()
}
}

doorImage2.onclick = () => {
if(!isClicked(doorImage2)) {
doorImage2.src = openDoor2;
playDoor()
}
}
doorImage3.onclick = () => {
if(!isClicked(doorImage3)) {
doorImage3.src = openDoor3;
playDoor()
}
}
//game is over changing the button
function gameOver(status) {
if (status === ‘win’) {
startButton.innerHTML = ‘You win! Play again?’;
}
}
//call of random doors
randomChoreDoorGenerator()

Do you mind using the preformatted text option for any code part when writing your posts? You can add the tags manually, too. Makes debugging easier for us.

```
multi
line
code
```

and

` single line code `

Your code seems to be working, the code that is causing the error you’re describing is this part:
const numClosedDoors = 3; (around line 10)

constant variables can’t be changed that easily, which you’re trying to do here:

//check is game winning condition
const playDoor = () => {
  numClosedDoors--;    // <----- 
  if (numClosedDoors === 0) {
    gameOver('win');
  }
}

in your case it must be created using let instead of const in order for the text ‘You win! Play again?’ to appear after opening all three doors. (no game over condition, and no restart condition, yet)

Thank you again, but I stuck on steps 63-65. It seems that when the bot image appears it does not influence on my button - You win…

//connection doors to html
const doorImage1 = document.getElementById('door1');
const doorImage2 = document.getElementById('door2');
const doorImage3 = document.getElementById('door3');
//images behind doors
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";
//number of doors
let numClosedDoors = 3;
//doors open
let openDoor1;
let openDoor2;
let openDoor3;

//current gaming logic var
let currentlyPlaying = true;

//door closed
const closedDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg";

//start button
let startButton = document.getElementById('start');

//checking if this door contain bot image
const  isBot = (door) => {
if(door.src === botDoorPath) {
  return true;
}  else {
  return false;
}
}
//checking if door opened once
const isClicked = (door) => {
if (door.src === closedDoorPath) {
  return false;
} else {
  return true;
}
}

//check is game winning condition
const playDoor = () => {
numClosedDoors--;
  if (numClosedDoors === 0) {
 gameOver('win');
} else if (isBot(door)) {
gameOver()  ;
} else {
  startButton.innerHTML = "Game over! Play again?";
}
}

//randomise the doors
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 = beachDoorPath;
    openDoor2 = spaceDoorPath;
  }
}

//when clicked you get new doors
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)  
  }
}
//game is over changing the button
function gameOver(status) {
 if (status === 'win') {
  startButton.innerHTML = 'You win! Play again?';
}
  currentlyPlaying = false;
}
//call of random doors
randomChoreDoorGenerator()

In your playDoor() function, you’re trying to pass a parameter door to isBot() but it never gets any because your playDoor() function doesn’t accept or use one.

//check is game winning condition
const playDoor = () => { // <----- 
numClosedDoors--;
  if (numClosedDoors === 0) {
 gameOver('win');
} else if (isBot(door)) { // <-----
gameOver()  ;
} else {
  startButton.innerHTML = "Game over! Play again?";
}
}

Thank you. But I still don’t understand why it does not working. What should I write in the PlayDoor()?

Here’s a hint: the actual winning condition check is happening in the gameOver() function.
(Dunno if I worded that right.)

Click for the solution:

Spoiler
//check is game winning condition
const playDoor = (door) => {
numClosedDoors--;
  if (numClosedDoors === 0) {
    gameOver('win');
  } else if (isBot(door)) {
    gameOver();
  }
}
//game is over changing the button
function gameOver(status) {
 if (status === 'win') {
    startButton.innerHTML = 'You win! Play again?';
  } else {
    startButton.innerHTML = "Game over! Play again?";
  }
  currentlyPlaying = false;
}
1 Like

Thank you so much! I managed to finish this project. Not east at all!

1 Like