Chore Door: step 8

Can someone explain me why both solutions to step 8 work? As far as I know the second one, which the task tells me to do, shouldn’t work, because I didn’t declare a ‘door1’ variable. So why does it work? And is the first solution better or doesn’t it matter?

Step 8

Inside the script.js file, underneath your global variable, assign door1.onclick to a new, empty arrow function.

Solution #1

let doorImage1 = document.getElementById("door1");
const botDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg";

doorImage1.onclick = () => {
  doorImage1.src = botDoorPath;
};

Solution #2

let doorImage1 = document.getElementById("door1");
const botDoorPath = "https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg";

door1.onclick = () => {
  doorImage1.src = botDoorPath;
};

Thanks already for helping me out!

2 Likes

You are indeed correct. And it has been addressed to the staff multiple times. Your solution 1 is the proper one, the cause of solution 2 working may be in the back-end of the exercise. Other people ask the same question ;). Goed bezig!

4 Likes

Cool, thnx! (And… bedankt voor de heads up!)

4 Likes

Just adding a bit more, I’m working on this project now and Solution 1 provided by @sylvesterhofstra doesn’t work at all for me, only Solution 2 works. Furthermore, the walk-through video uses solution 2 and as mentioned it’s not clear why how it even works as “door1” is never declared as a variable in the code.

Hey,

Could you provide your code so we can see if we can figure it out together ;)?