Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const animalType =
- {
- NONE : "none",
- PONEY : "poney",
- LION : "lion",
- KOALA : "koala",
- HUMAN : "human"
- }
- const legsCountByAnimalType = {
- [animalType.NONE] : 0,
- [animalType.PONEY] : 4,
- [animalType.LION] : 4,
- [animalType.KOALA] : 2,
- [animalType.HUMAN] : 2
- };
- class Animal
- {
- running = false;
- legsCount = 0;
- type = animalType.NONE;
- getLegsCount()
- {
- return (this.legsCount);
- }
- run()
- {
- this.running = true;
- }
- loseOneLeg()
- {
- this.legsCount--;
- if (this.legsCount < 0)
- {
- this.legsCount = 0;
- }
- }
- cureAllLegs()
- {
- this.legsCount = legsCountByAnimalType[this.type];
- }
- constructor(aParameterNamedTypeSomehow, running = false)
- {
- this.type = aParameterNamedTypeSomehow;
- this.legsCount = legsCountByAnimalType[this.type];
- this.running = running;
- }
- }
- const someLion = new Animal(animalType.LION);
- const someRunningLionUsingTheOptionnalParameter = new Animal(animalType.LION, true);
- console.log("how many legs have the lion??? \n" + someLion.getLegsCount());
- console.log("is the first lion running? \n" + someLion.running);
- console.log("is the second lion running? \n" + someRunningLionUsingTheOptionnalParameter.running);
- // Ah! lets make the first lion run as well!
- someLion.run();
- console.log("is the first lion running now please??? i want him to run! \n" + someLion.running);
- const someKoala = new Animal(animalType.KOALA);
- console.log("how many legs have my koala?\n" + someKoala.getLegsCount());
- // lets remove one leg from the koala he has been bitten by a spider
- someKoala.loseOneLeg();
- // please cure the koala!!! he is in danger in the wild!
- // add code to cure the koala after this line!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement