The robot lesson


#1

let doorImage1 = document.getElementById(‘door1’);
let doorImage2 = document.getElementById(‘door2’);
let doorImage3 = document.getElementById(‘door3’);
let botDoorPath = “LINK REMOVED DUE TO CODECADEMY RESTRICTIONS”
let beachDoorPath = ""LINK REMOVED DUE TO CODECADEMY RESTRICTION
let spaceDoorPath = “//s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg
let numClosedDoors = 3;
let openDoor1 = 0;
let openDoor2 = 1;
let openDoor3 = 2;
const randomChoreDoorGenerator = () => {
const choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;

}else if (choreDoor === 1) {
openDoor2 = beachDoorPath;
openDoor1 = botDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 2)
openDoor3 = spaceDoorPath;
openDoor2 = beachDoorPath;
openDoor1 = botDoorPath;
}
doorImage1.onclick = () => {
doorImage1.src = openDoor1;

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

SOMEONE KNOWS WHAT IS WRONG WITH THIS CODE ?
IT SHOUND RANDOMLY OPEN AN DIFERENT DOOR ON EACH CLICK BUT FOR NOW IT OPEN THE SAME IMAGE ON SAME DOOR IT SHOUOLD CHANGE THE IMAGE ON THE DOORS RAMDONLY.
APRECIATE ANY ANSWER :slight_smile: IM A NOOB


#2
if (choreDoor === 0) {
	openDoor1 = botDoorPath;
	openDoor2 = beachDoorPath;
	openDoor3 = spaceDoorPath;
}else if (choreDoor === 1) {
	openDoor2 = beachDoorPath;
	openDoor1 = botDoorPath;
	openDoor3 = spaceDoorPath;
} else if (choreDoor === 2){
	openDoor3 = spaceDoorPath;
	openDoor2 = beachDoorPath;
	openDoor1 = botDoorPath;
}

Your problem lies in this part. The variables are in a diffrent order but you still set each variable to the same path.

openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;

#3

wow thanks for reply , like this now but im stil confused kkk

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

}else if (choreDoor === 1) {
openDoor3 = spaceDoorPath;
openDoor2 = beachDoorPath;
openDoor1 = botDoorPath;

} else if (choreDoor === 2)
openDoor3 = botDoorPath;
openDoor2 = spaceDoorPath;
openDoor1 = beachDoorPath;

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

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


#4
if (choreDoor === 0) {
		openDoor1 = botDoorPath;
		openDoor2 = beachDoorPath;
		openDoor3 = spaceDoorPath;
	}else if (choreDoor === 1) {
		openDoor1 = spaceDoorPath;
		openDoor2 = botDoorPath;
		openDoor3 = beachDoorPath;
	} else if (choreDoor === 2)       //<---- Missing '{'
		openDoor1 = beachDoorPath;
		openDoor2 = spaceDoorPath;
		openDoor3 = botDoorPath;
	}

You’re missing a ‘{’ in your code. This makes for unexpected results. I commented where it should go.

also in what you posted:

if (choreDoor === 0) {
   openDoor1 = spaceDoorPath;
   openDoor2 = botDoorPath;
   openDoor3 = spaceDoorPath;
}

check the variables one more time.


#5

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 = 0;
let openDoor2 = 1;
let openDoor3 = 2;
const randomChoreDoorGenerator = () => {
const choreDoor = Math.floor(Math.random() * numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;

}else if (choreDoor === 1) {
openDoor3 = botDoorPath;
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;

} else if (choreDoor === 2)
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;

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

}
doorImage2.onclick = () => {
doorImage2.src = openDoor2;
}
doorImage3.onclick = () => {
doorImage3.src = openDoor3;
}
randomChoreDoorGenerator();[quote=“biirra, post:4, topic:379315, full:true”]

if (choreDoor === 0) {
		openDoor1 = botDoorPath;
		openDoor2 = beachDoorPath;
		openDoor3 = spaceDoorPath;
	}else if (choreDoor === 1) {
		openDoor1 = spaceDoorPath;
		openDoor2 = botDoorPath;
		openDoor3 = beachDoorPath;
	} else if (choreDoor === 2)       //<---- Missing '{'
		openDoor1 = beachDoorPath;
		openDoor2 = spaceDoorPath;
		openDoor3 = botDoorPath;
	}

You’re missing a ‘{’ in your code. This makes for unexpected results. I commented where it should go.
[/quote]

so far its working like this ,


#6

Glad to help.

Try to console.log() some variables next time something wierd happens. its a good way of finding out what stuff is doing. More information on console.log() can be found here.


#7

if (choreDoor === 0) {
openDoor1 = beachDoorPath;
openDoor2 = botDoorPath;
openDoor3 = spaceDoorPath;

thanks man :slight_smile: just one more question if u can .
look this one for exemple .
when i click the corresponding dor to index 0 Doornumer1 it show random beach and botDoor image but never shows the space image why if all 3 is in the if ? … /this is for u birra i coundt reply anymore --> codycademy restriction on numbers of reply



merged threads, tagging @biirra for good measure
// @ionatan


#8

Check your code again. Keep an eye out for missing }. Make sure every { has a closing }

Javascript does not throw an error when this stuff happens so always check

Also, it is not wise to ask a question to a single person. My attendence on this forum is very wobly. I could be gone for weeks without notice. This would mean your question would probably never get an answer.

So be sure to form your question so anyone on the forum could help without needing to read other threads.