Skip to content

Instantly share code, notes, and snippets.

@thuyanduong
Created January 4, 2022 20:52
Show Gist options
  • Select an option

  • Save thuyanduong/cf516fc35ae454df76c048d7773d741e to your computer and use it in GitHub Desktop.

Select an option

Save thuyanduong/cf516fc35ae454df76c048d7773d741e to your computer and use it in GitHub Desktop.
Simple OO Neopets

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!

  1. Create a factory function neopetsFactory() that takes in a name and returns an object.
  2. Objects returned from the function have three data properties, name which is the argument provided, happiness which is initialized to 100 and hunger with is initizlied to 50. There is no min nor max value for happiness and hunger.
  3. Objects returned from the function have three methods: play(), eatVeggies() and eatJunkFood().
  4. 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!".
  5. 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!".
  6. 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;        // 45

Bonus: Convert to ES6 Classes

Recreate 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment