Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //OUTPUT: An instance of DOG. An object with properties
- // const dog = {
- // name: 'Scout',
- // breed: ['Husky', 'German Shepherd'],
- // age: 4,
- // happiness: 50,
- // hunger: 10,
- // energy: 100,
- // };
- //functional class?
- var Dog = function(dogName, breed, age) {
- var dogObj = {};
- dogObj.dogName = dogName;
- dogObj.breed = breed;
- dogObj.age = age;
- dogObj.happiness = statGen();
- dogObj.hunger = statGen();
- dogObj.energy = statGen();
- extend(dogObj, Dog.methods);
- return dogObj;
- }
- //generates a number rating ranging from 0-100
- var statGen = function() {
- return Math.floor(Math.random() * 101);
- }
- var extend = function (obj1, obj2) {
- for (var key in obj2) {
- obj1[key] = obj2[key];
- }
- }
- Dog.methods = {
- //method of dog called feed
- feed: function (food) {
- if(this.hunger - food > 0) {
- this.hunger -= food;
- } else {
- this.hunger = 0;
- }
- },
- //method of dog called play
- play: function(time) {
- if(this.happiness + time < 100) {
- this.happiness += time;
- } else {
- this.happiness = 100;
- }
- if(this.energy - time > 0) {
- this.energy -= time;
- } else {
- this.energy = 0;
- }
- },
- //method of dog called nap
- nap :function(time) {
- if(this.energy + time < 100) {
- this.energy += time;
- } else {
- this.energy = 100;
- }
- }
- }
- Dog("Coco", ["husky", "corgi"], 2);
Add Comment
Please, Sign In to add comment