Guest User

Untitled

a guest
Oct 18th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. //OUTPUT: An instance of DOG. An object with properties
  2. // const dog = {
  3. // name: 'Scout',
  4. // breed: ['Husky', 'German Shepherd'],
  5. // age: 4,
  6. // happiness: 50,
  7. // hunger: 10,
  8. // energy: 100,
  9. // };
  10.  
  11.  
  12. //functional class?
  13. var Dog = function(dogName, breed, age) {
  14. var dogObj = {};
  15. dogObj.dogName = dogName;
  16. dogObj.breed = breed;
  17. dogObj.age = age;
  18. dogObj.happiness = statGen();
  19. dogObj.hunger = statGen();
  20. dogObj.energy = statGen();
  21. extend(dogObj, Dog.methods);
  22. return dogObj;
  23. }
  24.  
  25. //generates a number rating ranging from 0-100
  26. var statGen = function() {
  27. return Math.floor(Math.random() * 101);
  28. }
  29.  
  30. var extend = function (obj1, obj2) {
  31. for (var key in obj2) {
  32. obj1[key] = obj2[key];
  33. }
  34. }
  35.  
  36. Dog.methods = {
  37. //method of dog called feed
  38. feed: function (food) {
  39. if(this.hunger - food > 0) {
  40. this.hunger -= food;
  41. } else {
  42. this.hunger = 0;
  43. }
  44. },
  45.  
  46. //method of dog called play
  47. play: function(time) {
  48. if(this.happiness + time < 100) {
  49. this.happiness += time;
  50. } else {
  51. this.happiness = 100;
  52. }
  53.  
  54. if(this.energy - time > 0) {
  55. this.energy -= time;
  56. } else {
  57. this.energy = 0;
  58. }
  59. },
  60.  
  61. //method of dog called nap
  62. nap :function(time) {
  63. if(this.energy + time < 100) {
  64. this.energy += time;
  65. } else {
  66. this.energy = 100;
  67. }
  68. }
  69.  
  70. }
  71.  
  72. Dog("Coco", ["husky", "corgi"], 2);
Add Comment
Please, Sign In to add comment