Please, help me to find an error in my js code :)

I am working on the Chore Door exercise in Javascript Interactive Website course. When clicking on the doors, my doors don’t open (the images behind the doors don’t load).

Please, help me to find out, why!

let doorImage1 = document.getElementById(“door1”);
let doorImage2 = document.getElementById(“door2”);
let doorImage3 = document.getElementById(“door3”);

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 numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;

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

door1.onclick = () => {
doorImage1.src=openDoor1;
};
door2.onclick = () => {
doorImage2.src=openDoor2;
};
door3.onclick = () => {
doorImage3.src=openDoor3;
};

randomChoreDoorGenerator();
1 Like

Hi Oligaiv,

Taking a look at the code I can see a few things that stand out to me which I will list for you.

  • Double quotes should be single quotes within your variables

  • Your If/Else has an argument on the else - you don’t need to specify choreDoor === 2 because that is the only possible outcome should the other two fail.

  • The openDoor variables on the if/else if/else statement do not need let in front of them as you have already initialised the variables above the statement itself.

Hopefully that should help you :slight_smile:

1 Like

This one makes no difference, you can use both.

The rest of your points are very valid :wink:

2 Likes

Ohhhh! Many many thanks! You’ve made my evening! It is working!!! huraaa ;)))