Simple OO Neopets
Neopets are virtual pets that you can play and interact with. The better you take care of them, the happier and healthy they will be!
- Create a factory function
neopetsFactory()that takes in a name and returns an object. - Objects returned from the function have three data properties,
namewhich is the argument provided, happiness which is initialized to100and hunger with is initizlied to50. There is no min nor max value forhappinessandhunger. - Objects returned from the function have three methods:
play(),eatVeggies()andeatJunkFood(). play()increases their happiness and also makes them more hungry. This method adds 4 points to happiness and adds 5 points to hunger and returns"Weee!".eatVeggies()decreases their hunger, but no one enjoys eatting veggies. This method subtracts 6 points from happiness and subtracts 8 points from hunger and returns"Bleh!".eatJunkFood()makes neopets happy, but isn't very filling. This method adds 3 points to happiness and subtracts 2 points from hunger and returns"Yumm!".
const pet = neopetsFactory("Krawk");
pet.name; // "Krawk"
pet.happiness; // 100
pet.hunger; // 50
pet.play(); // "Weee!"
pet.happiness; // 104
pet.hunger; // 55
pet.eatVeggies(); // "Bleh!"
pet.happiness; // 98
pet.hunger; // 47
pet.eatJunkFood(); // "Yumm!"
pet.happiness; // 101
pet.hunger; // 45Recreate the above exercise using ES6 Classes. Create a class called Neopet where isntances have the same data properties and methods as above.
const pet = new Neopet("Krawk");
pet.name; // "Krawk"
pet.happiness; // 100
pet.hunger; // 50
pet.play(); // "Weee!"
pet.happiness; // 104
pet.hunger; // 55
pet.eatVeggies(); // "Bleh!"
pet.happiness; // 98
pet.hunger; // 47
pet.eatJunkFood(); // "Yumm!"
pet.happiness; // 101
pet.hunger; // 45