Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- CODE CHALLENGES: INTERMEDIATE JAVASCRIPT
- dogFactory()
- Instructions
- 1.
- Write a function, dogFactory(). It should:
- have 3 parameters: name, breed, and weight (in that order)
- expect name and breed to be strings
- expect weight to be a number
- return an object
- have each of those parameters as keys on the returned object returned with the values of the arguments that were passed in
- dogFactory('Joe', 'Pug', 27)
- // Should return { name: 'Joe', breed: 'Pug', weight: 27 }
- 2.
- Add getters and setters for each of the three properties and change the property names to have an underscore prepended.
- 3.
- Add two methods to your object: .bark() which returns ‘ruff! ruff!’ and .eatTooManyTreats() which should increment the weight property by 1.
- */
- // Write your code here:
- function dogFactory(name, breed, weight){
- return {
- _name: name,
- _breed: breed,
- _weight: weight,
- get name(){
- return this._name;
- },
- get breed(){
- return this._breed;
- },
- get weight(){
- return this._weight;
- },
- set name(n){
- this._name = n;
- },
- set breed(b){
- this._breed = b;
- },
- set weight(w){
- this._weight = w;
- },
- // METHODS
- bark(){
- return "ruff! ruff!";
- },
- eatTooManyTreats(){
- this.weight += 1;
- }
- };
- }
- console.log(dogFactory('Joe', 'Pug', 27));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement