Whenever I click on any door it always shows the bot first and the game ends. I have looked over my code again and again and I cant tell whats wrong. Everything else works fine as far as I know. Here is my js code, please help me figure out what the problem is:
let doorImage1 = document.getElementById(‘door1’);
let doorImage2 = document.getElementById(‘door2’);
let doorImage3 = document.getElementById(‘door3’);
let startButton = document.getElementById(‘start’);
let botDoorPath = ‘link’’
let beachDoorPath = ‘link’
let spaceDoorPath = ‘link’
const closedDoorPath = ‘link’
let numClosedDoors = 3;
let openDoor1
let openDoor2
let openDoor3
let currentlyPlaying = true;
const isBot = door => {
if (door.src = botDoorPath) {
return true;
} else {
return false;
}
}
const isClicked = (door) => {
if (door.src = closedDoorPath) {
return false;
} else {
return true;
}
}
const playDoor = (door) => {
numClosedDoors–;
if (numClosedDoors === 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) {
openDoor1 = spaceDoorPath;
openDoor2 = botDoorPath;
openDoor3 = beachDoorPath;
} else if (choreDoor === 2) {
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
openDoor3 = botDoorPath;
}
}
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);
}
}
startButton.onclick = () => {
startRound();
}
const startRound = () => {
numClosedDoors = 3;
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
startButton.innerHTML = ‘Good Luck!’
currentlyPlaying = true;
randomChoreDoorGenerator();
}
const gameOver = (status) => {
if (status === ‘win’) {
startButton.innerHTML = ‘You win! Play again?’
} else {
startButton.innerHTML = ‘Game over! Play again?’
}
currentlyPlaying = false;
}
NOTE: the links are entered correctly in my actual js file but I couldn’t post it here.
i am having the exact same problem and cant figure it out either. I even deleted the whole assignment and started from scratch only to have the same thing occur.
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 openDoor1;
let openDoor2;
let openDoor3;
let numClosedDoors = 3;
let currentlyPlaying = true;
const randomChoreDoorGenerator = () => {
let choreDoor = Math.floor(Math.random() * numOfClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor2 = botDoorPath;
openDoor1 = spaceDoorPath;
openDoor3 = beachDoorPath;
} else (choreDoor === 2)
openDoor3 = botDoorPath;
openDoor2 = spaceDoorpath;
openDoor1 = beachDoorPath;
};
const isBot = (door) => {
if (door.src = botDoorPath) {
return true;
} else {
return false;
}
};
const isClicked = (door) => {
if (door.src === closedDoorPath) {
return false;
} else {
return true;
}
};
const playDoor = (door) => {
numClosedDoors--;
if (numClosedDoors === 0) {
gameOver('win');
} else if (isBot(door)) {
gameOver();
}
};
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);
}
};
startButton.onclick = () => {
if (currentlyPlaying === false){
startRound();
}
};
const startRound = () => {
numClosedDoors = 3;
doorImage.src = closedDoorPath;
startButton.innerHTML = 'Good Luck!';
randomChoreDoorGenerator();
};
const gameOver = (status) => {
if (status === 'win') {
startButton.innerHTML = 'You Win! Play again?';
} else {
startButton.innerHTML = 'Game Over! Play again?';
} currentlyPlaying = false;
};
startRound();
@script8252849137 and @ketchell21
Post your code using the code format button </> .
For a full explanation please go here:
To format code so it’s visible, either wrap it in single backticks for a small amount of code, or triple backticks on an otherwise blank line before and after for a large amount of code.
1 Like
First of all an else statement does not have a condition.
And also, the actions are not between {}.
} else {
openDoor3 = botDoorPath;
openDoor2 = spaceDoorpath;
openDoor1 = beachDoorPath;
}
If you run into more problems, don’t hesitate to ask. Happy coding!
1 Like
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'
const 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;
const isBot = (door) => {
if (door.src = botDoorPath) {
return true;
} else {
return false;
}
}
const isClicked = (door) => {
if (door.src = closedDoorPath) {
return false;
} else {
return true;
}
}
const playDoor = (door) => {
numClosedDoors--;
if (numClosedDoors === 0) {
gameOver('win');
} else if (isBot(door)) {
gameOver('lose');
}
}
const randomChoreDoorGenerator = () => {
choreDoor = Math.floor(Math.random()*numClosedDoors);
if (choreDoor === 0) {
openDoor1 = botDoorPath;
openDoor2 = beachDoorPath;
openDoor3 = spaceDoorPath;
} else if (choreDoor === 1) {
openDoor1 = spaceDoorPath;
openDoor2 = botDoorPath;
openDoor3 = beachDoorPath;
} else if (choreDoor === 2) {
openDoor1 = beachDoorPath;
openDoor2 = spaceDoorPath;
openDoor3 = botDoorPath;
}
}
door1.onclick = () => {
if (currentlyPlaying && !isClicked(door1)) {
doorImage1.src = openDoor1;
playDoor(door1);
}
}
door2.onclick = () => {
if (currentlyPlaying && !isClicked(door2)) {
doorImage2.src = openDoor2;
playDoor(door2);
}
}
door3.onclick = () => {
if (currentlyPlaying && !isClicked(door3)) {
doorImage3.src = openDoor3;
playDoor(door3);
}
}
startButton.onclick = () => {
if (!currentlyPlaying) {
startRound();
}
}
const startRound = () => {
numClosedDoors = 3;
doorImage1.src = closedDoorPath;
doorImage2.src = closedDoorPath;
doorImage3.src = closedDoorPath;
startButton.innerHTML = 'Good Luck!'
currentlyPlaying = true;
randomChoreDoorGenerator();
}
const gameOver = (status) => {
if (status === 'win') {
startButton.innerHTML = 'You win! Play again?'
} else {
startButton.innerHTML = 'Game over! Play again?'
}
currentlyPlaying = false;
}
startround();
Here is my code please let me know if you find whats wrong. Thank you!
1 Like
script8252849137:
startround();
The error lies in here Lets see if you can figure out whats wrong.
I get the following error:
Reference error: startround is not defined
2 Likes
A next error is found in some of your if statements
if (door.src = botDoorPath)
One =
is used in a reassignments. You need either ==
or ===
to provide the if statement with a condition to test.
2 Likes
Thanks so much! Once I fixed my ===
my game started working, but only after the first round. This is when I realized what you were talking about in your other post. I cant believe I forgot my camelCase haha. Thanks for spotting my silly errors.
2 Likes
system
Closed
January 10, 2020, 12:01pm
10
This topic was automatically closed 18 hours after the last reply. New replies are no longer allowed.