Advertisement
Guest User

Untitled

a guest
Jan 21st, 2020
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const animalType =
  2. {
  3.     NONE : "none",
  4.     PONEY : "poney",
  5.     LION : "lion",
  6.     KOALA : "koala",
  7.     HUMAN : "human"
  8. }
  9.  
  10. const legsCountByAnimalType = {
  11.     [animalType.NONE] : 0,
  12.     [animalType.PONEY] : 4,
  13.     [animalType.LION] : 4,
  14.     [animalType.KOALA] : 2,
  15.     [animalType.HUMAN] : 2
  16. };
  17.  
  18. class Animal
  19. {
  20.     running = false;
  21.     legsCount = 0;
  22.     type = animalType.NONE;
  23.     getLegsCount()
  24.     {
  25.         return (this.legsCount);
  26.     }
  27.     run()
  28.     {
  29.         this.running = true;
  30.     }
  31.     loseOneLeg()
  32.     {
  33.         this.legsCount--;
  34.         if (this.legsCount < 0)
  35.         {
  36.             this.legsCount = 0;
  37.         }
  38.     }
  39.     cureAllLegs()
  40.     {
  41.         this.legsCount = legsCountByAnimalType[this.type];
  42.     }
  43.     constructor(aParameterNamedTypeSomehow, running = false)
  44.     {
  45.         this.type = aParameterNamedTypeSomehow;
  46.         this.legsCount = legsCountByAnimalType[this.type];
  47.         this.running = running;
  48.     }
  49.  
  50. }
  51.  
  52. const someLion = new Animal(animalType.LION);
  53.  
  54. const someRunningLionUsingTheOptionnalParameter = new Animal(animalType.LION, true);
  55.  
  56. console.log("how many legs have the lion??? \n" + someLion.getLegsCount());
  57.  
  58. console.log("is the first lion running? \n" + someLion.running);
  59. console.log("is the second lion running? \n" + someRunningLionUsingTheOptionnalParameter.running);
  60.  
  61. // Ah! lets make the first lion run as well!
  62.  
  63. someLion.run();
  64.  
  65. console.log("is the first lion running now please??? i want him to run! \n"  + someLion.running);
  66.  
  67.  
  68. const someKoala = new Animal(animalType.KOALA);
  69.  
  70. console.log("how many legs have my koala?\n" + someKoala.getLegsCount());
  71.  
  72. // lets remove one leg from the koala he has been bitten by a spider
  73.  
  74. someKoala.loseOneLeg();
  75.  
  76. // please cure the koala!!! he is in danger in the wild!
  77.  
  78. // add code to cure the koala after this line!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement