Chore dors javascript exercice : my script does not work

Hello,

Nothing appen when i click on a door. I didn’t modify the html file.
https://www.codecademy.com/courses/build-interactive-websites/projects/chore-door-revised.

Would someone understand what’s wrong in it ?

// Access HTML elements

let doorImage1 = document.getElementById('door1');
let doorImage2 = document.getElementById('door2');
let doorImage3 = document.getElementById('door3');
let startButton = document.getElementById('start');

let botDoorPath = 'https://content.codecademy.com/projects/chore-door/images/robot.svg';
let beachDoorPath = 'https://content.codecademy.com/projects/chore-door/images/beach.svg';
let spaceDoorPath = 'https://content.codecademy.com/projects/chore-door/images/space.svg';
let closedDoorPath = 'https://content.codecademy.com/projects/chore-door/images/closed_door.svg';

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

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

const gameOver = (status) => {
  (if status === 'win'){
    startButton.innerHTML= 'You win! Play again?';
  }
  else {
    startButton.innerHTML= 'Game over! Play again?';
  }
  currentlyPlaying=false;
}

const playDoor = (door) => {
  numClosedDoors--;
  if(numClosedDoors===0){
    gameOver('win');
  }
  else if (isBot(door)) {
    gameOver();
  }
}

const randomChoreDoorGenerator = () => {
  let choreDoor = Math.floor(Math.ramdom() * numClosedDoors);

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

}


let numClosedDoors = 3;
let openDoor1;
let openDoor2;
let openDoor3;
let currentlyPlaying = true;

// Define game logic to check doors, progress game, end game, and choose a random chore door

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();
  }
}

// Start a game round
const startRound = () => {
  doorImage1.src = closedDoorPath;
  doorImage2.src = closedDoorPath;
  doorImage3.src = closedDoorPath;
  numClosedDoors = 3;
  currentlyPlaying = true;
  startButton.innerHTML = 'Good Luck!';
  randomChoreDoorGenerator(); 
}

startRound();

In the gameOver function, you have a ( on the wrong side of the if

Also, you used openDoor1, openDoor2, openDoor3, numClosedDoors before you declared them.
(These declarations should probably be before the gameOver and playDoor and randomChoreDoorGenerator functions.)

In the randomChoreDoorGenerator function, Math.random() is misspelled.

(Note that I used the console in the browser’s “Developer Tools” to help debug this stuff.)

2 Likes

Thank you very much @[janbazant1107978602]
It works now ! :grinning: (even if i don’t declare var before i use it - but i wll be carefull about this point next time).
I will check my code with de browser console next time also.