Advertisement
makispaiktis

Codecademy - 24th Exercise (Chore Door - JS)

Jan 1st, 2020 (edited)
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Global variables
  2. // Door <img> elements and their changed paths
  3. let doorImage1 = document.getElementById('door1');
  4. let botDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/robot.svg';
  5. let doorImage2 = document.getElementById('door2');
  6. let beachDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/beach.svg';
  7. let doorImage3 = document.getElementById('door3');
  8. let spaceDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/space.svg';
  9. let closedDoorPath = 'https://s3.amazonaws.com/codecademy-content/projects/chore-door/images/closed_door.svg';
  10. // Create global variables for having randomness in robot hiding
  11. let numClosedDoors = 3;
  12. let openDoor1;
  13. let openDoor2;
  14. let openDoor3;
  15. // Start Button
  16. let startButton = document.getElementById('start');
  17. // currentlyPlaying
  18. let currentlyPlaying = true;
  19.  
  20.  
  21. // Functions
  22. // isBot()
  23. function isBot(door){
  24.   if(door.src === botDoorPath){
  25.     return true;
  26.   }
  27.   else{
  28.     return false;
  29.   }
  30. }
  31.  
  32. // isClicked()
  33. function isClicked(door){
  34.   if(door.src === closedDoorPath){
  35.     return false;
  36.   }
  37.   else{
  38.     return true;
  39.   }
  40. }
  41.  
  42. // playdoor()
  43. function playDoor(door){
  44.   numClosedDoors--;
  45.   if(numClosedDoors === 0){
  46.     gameOver('win');
  47.   }
  48.   else if(isBot(door) === true){
  49.     gameOver();
  50.   }
  51. }
  52.  
  53. // randomChoreDoorGenerator()
  54. const randomChoreDoorGenerator = () => {
  55.   let choreDoor = Math.floor(Math.random() * numClosedDoors);
  56.   if(choreDoor === 0){
  57.     openDoor1 = botDoorPath;
  58.     openDoor2 = beachDoorPath;
  59.     openDoor3 = spaceDoorPath;
  60.   }
  61.   else if(choreDoor === 1){
  62.     openDoor2 = botDoorPath;
  63.     openDoor1 = beachDoorPath;
  64.     openDoor3 = spaceDoorPath;
  65.   }
  66.   else if(choreDoor === 2){
  67.     openDoor3 = botDoorPath;
  68.     openDoor1 = beachDoorPath;
  69.     openDoor2 = spaceDoorPath;
  70.   }
  71. }
  72.  
  73.  
  74.  
  75. // onclick functions
  76. doorImage1.onclick = () => {
  77.   if(isClicked(doorImage1) === false && currentlyPlaying === true){
  78.     doorImage1.src = openDoor1;
  79.     playDoor(doorImage1);
  80.   }
  81. }
  82.  
  83. doorImage2.onclick = () => {
  84.   if(isClicked(doorImage2) === false && currentlyPlaying === true){
  85.     doorImage2.src = openDoor2;
  86.     playDoor(doorImage2);
  87.   }
  88. }
  89.  
  90. doorImage3.onclick = () => {
  91.   if(isClicked(doorImage3) === false && currentlyPlaying === true){
  92.     doorImage3.src = openDoor3;
  93.     playDoor(doorImage3);
  94.   }
  95. }
  96.  
  97. startButton.onclick = () => {
  98.   if(currentlyPlaying === false){
  99.     startRound();
  100.   }
  101. }
  102.  
  103. // startRound()
  104. function startRound(){
  105.   doorImage1.src = closedDoorPath;
  106.   doorImage2.src = closedDoorPath;
  107.   doorImage3.src = closedDoorPath;
  108.   numClosedDoors = 3;
  109.   startButton.innerHTML = 'Good Luck!';
  110.   currentlyPlaying = true;
  111.   randomChoreDoorGenerator();
  112. }
  113.  
  114. // gameOver()
  115. function gameOver(status){
  116.   if(status === 'win'){
  117.     startButton.innerHTML = 'You win! Play again?';
  118.   }
  119.   else{
  120.     startButton.innerHTML = 'Game Over! Play again?';
  121.   }
  122.   currentlyPlaying = false;
  123. }
  124.  
  125. startRound();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement