Advertisement
makispaiktis

Codecademy - Intermediate Javascript (Dog Factory)

Dec 25th, 2019 (edited)
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. CODE CHALLENGES: INTERMEDIATE JAVASCRIPT
  4. dogFactory()
  5. Instructions
  6. 1.
  7. Write a function, dogFactory(). It should:
  8.  
  9. have 3 parameters: name, breed, and weight (in that order)
  10. expect name and breed to be strings
  11. expect weight to be a number
  12. return an object
  13. have each of those parameters as keys on the returned object returned with the values of the arguments that were passed in
  14. dogFactory('Joe', 'Pug', 27)
  15. // Should return { name: 'Joe', breed: 'Pug', weight: 27 }
  16. 2.
  17. Add getters and setters for each of the three properties and change the property names to have an underscore prepended.
  18.  
  19. 3.
  20. Add two methods to your object: .bark() which returns ‘ruff! ruff!’ and .eatTooManyTreats() which should increment the weight property by 1.
  21.  
  22. */
  23.  
  24. // Write your code here:
  25. function dogFactory(name, breed, weight){
  26.   return {
  27.     _name: name,
  28.     _breed: breed,
  29.     _weight: weight,
  30.    
  31.     get name(){
  32.       return this._name;
  33.     },
  34.     get breed(){
  35.       return this._breed;
  36.     },
  37.     get weight(){
  38.       return this._weight;
  39.     },
  40.    
  41.     set name(n){
  42.       this._name = n;
  43.     },
  44.     set breed(b){
  45.       this._breed = b;
  46.     },
  47.   set weight(w){
  48.       this._weight = w;
  49.     },
  50.    
  51.     // METHODS
  52.     bark(){
  53.       return  "ruff! ruff!";
  54.     },
  55.     eatTooManyTreats(){
  56.       this.weight += 1;
  57.     }
  58.   };
  59. }
  60.  
  61. console.log(dogFactory('Joe', 'Pug', 27));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement