FAQ: Code Challenges: Intermediate JavaScript - dogFactory()

This community-built FAQ covers the “dogFactory()” exercise from the lesson “Code Challenges: Intermediate JavaScript”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

FAQs on the exercise dogFactory()

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hi,
I dont get the solution.

It says that the first two parameters have to be strings.
So I made:
set name (newName){
if( typeof newName===‘string’)
this.name=newName}
else{
console.log{please give a valid name},

in the solution there is non of these

1 Like
const dogFactory=(name,breed,weight)=>{ 
return  { _name: name,
       get name(){
    return name=this.name},
    set name(newName){
      if(typeof newName==='string')
       {return this.name=newName
      }else{
        console.log('please give a valid breed!')
    }},
     _breed:breed,
    get breed(){
      return breed=this.breed},
       set breed(newBreed){
      if(typeof newBreed==='string'){
        return this.breed=newBreed
      } else{
        console.log('please give a valid breed!')
      }},
     _weight: weight,
		  get weight(){
      return weight=this.weight
    },
     set weight(newWeight){
       if(newWeight=== 'number'){
         return this.weight=newWeight}
       else {console.log('please giva a valid age')}
       },
     bark(){console.log('ruff!ruff!')},
     EatTooManyTreats(){return this.weight++}
     }}

so that would be my code, but not accepted. No error messages, so can anybody help what is wrong with this?
Something is wrong but I cant figure what.
console shows this after trying out with given example:

{ _name: ‘Joe’,
name: [Getter/Setter],
_breed: ‘Pug’,
breed: [Getter/Setter],
_weight: 27,
weight: [Getter/Setter],
bark: [Function: bark],
EatTooManyTreats: [Function: EatTooManyTreats] }

Ok, there is no problem with my code, the solution code gives the same result. The first point is misleading. Thats the correct way of checking if it worked:

const dog = dogFactory('Joe', 'Pug', 27)

const {name} = dog
console.log(name)

Except, that it still takes any entry. I tried to give the name 12, and it returns 12. Any help on this?

1 Like

hi guys,

i don’t understand how to check my code

const dogFactory = (name, breed, weight) => {
return {
_name: name,
_breed: breed,
_weight: weight,
get name() {
return this._name;
},
set name(newName) {
this._name = newName;
},
get breed() {
return this._breed;
},
set breed(newBreed) {
this._breed = newBreed;
},
get weight() {
return this._weight;
},
set weight(newWeight) {
this._weight = newWeight;
},
bark() {
return ‘ruff! ruff!’
},
eatTooManyTreats() {
this._weight++
}
}
}

what exactly should I write after it to check it working in the console??

Yeah, I’m also confused about this one.
I followed the first instructions and programmed my setters to only accept string or number types, just like in the example lesson they linked too, but the code was rejected and the answer didn’t have any type checking in it.
For example, my code:

set name(newName) {
      if (typeof newName === 'string') {
        this._name = newName;
    } else {
        console.log('You must assign a string to name');

Versus their code:

set name(newName) {
    this._name = newName;
},

It’s left me a little annoyed, because it kept rejecting my answer even though I tested and got correct returns. I eventually just had it give me the “correct” answer so I could move on.

One final question. Why is it that when you type something like
const dog1 = dogFactory('Fluffy', 'Husky', 15); you get a return of:
Screen Shot 2020-02-25 at 6.07.46 PM
Whereas when you type something like:
const dog1 = dogFactory('Fluffy', 'Husky', 15); You get a more expected return.
Screen Shot 2020-02-25 at 6.08.02 PM

Here’s my code for completeness’ sake:

Long-ish Code Snippet
const dogFactory = (name, breed, weight) => {
  return {
    // Name Property, Getter, Setter
    _name: name,
    get name() {
      return this._name;
    },
    set name(newName) {
      if (typeof newName === 'string') {
        this._name = newName;
    } else {
        console.log('You must assign a string to name');
      }
    },
    // Breed Property, Getter, Setter
    _breed: breed,
    get breed() {
      return this._breed;
    },
    set breed(newBreed) {
      if (typeof newBreed === 'string') {
        this._breed = newBreed;
    } else {
        console.log('You must assign a string to breed');
      }
    },
    // Weight Property, Getter, Setter
    _weight: weight,
    get weight () {
      return this._weight;
    },
    set weight(newWeight) {
      if (typeof newWeight === 'number') {
        this._weight = newWeight;
    } else {
        console.log('You must assign a number to weight');
      }
    },
    // Dog Methods
    bark() {
      return 'ruff! ruff!'
    },
    eatTooManyTreats() {
      this._weight++
    },
  }
};
const dog1 = dogFactory('Fluffy', 'Husky', 15);

console.log(dog1);

console.log(dog1.name, dog1.breed, dog1.weight);
dog1.eatTooManyTreats();
console.log(dog1.weight);
dog1.name = 7;
dog1.breed = 7;
dog1.weight = '';

Thank you for any info you can give.

3 Likes

// Write your code here:
const dogFactory = (name, breed, weight) =>{
return {
_name: name,
_breed: breed,
_weight: weight,
get name(){
return this._name;
},
set name(newName){
this._name = newName;
},
get breed(){
return this._breed;
},
set breed(newBreed){
this._breed = newBreed;
},
get weight(){
return this._weight;
},
set name(newWeight){
this._weight = newWeight;
},
bark() {
return ‘ruff! ruff!’
},
eatTooManyTreats() {
this._weight++
}
}
}
why this code for given exercise is not getting passed it is showing error: Did you add a name setter method for the _name property?

Can someone please tell me what the error in my code is. Error says unexpected identified ‘name’ in get name()

const dogFactory = (name, breed, weight) => {
if((typeof name === ‘string’) && (typeof breed === ‘string’) && (typeof weight === ‘number’)){
return{
name,
breed,
weight,
}},
get name() {
return this.name;
},
get breed() {
return this.breed;
},
get weight(){
return this.weight;
},
set name(name1) {
this.name = name1;
},
set breed(breed1) {
this.breed = breed1;
},
set weight(weight1) {
this.weight = weight1;
},

};

Whuut??? So other than being a little better at typing now, what exactly was the purpose of that last lesson?

This is another one of those lessons that pass the test but teaches nothing.

Here is my code:

 const dogFactory = (name, breed, weight) => {

return {

_name: name,

get name() {

  return this._name;

},

set name(name) {

  this._name = name;

},

_breed: breed,

get breed() {

  return this._breed;

},

set breed(breed) {

  this._breed  = breed;

},

_weight: weight,

get weight() {

  return this._weight;

},

set weight(weight) {

  this._weight = weight;

},

bark() {

  return 'ruff! ruff!';

},

eatTooManyTreats() {

  this.weight++;

}

} 

}

const myDog = dogFactory('Fred', 'Mutt', 110);

console.log(myDog)

And the output:

{ _name: 'Fred',
  name: [Getter/Setter],
  _breed: 'Mutt',
  breed: [Getter/Setter],
  _weight: 110,
  weight: [Getter/Setter],
  bark: [Function: bark],
  eatTooManyTreats: [Function: eatTooManyTreats] }

What am I to take away from this? Some of these courses feel like they were not fully completed. Or am I just expecting to much from the lessons? Or am I misunderstanding how to complete them?

Most of the lessons are great and a very good challenge. But when this one got to the end i was extremely confused. I feel like I did something wrong on this one. Anyone have any insight.

Edits to correct my formatting and finish my last paragraphs.

9 Likes

We can add a print() method to our object.

,
    print () {
      console.log(`${this.name}\n${this.breed}\n${this.weight}\n${this.bark()}`)
    }

Now create an instance,

rex = dogFactory('Rex','Labrador',60)

and now run its print method…

rex.print()

Output,

Rex
Labrador
60
ruff! ruff!
5 Likes

‘return’ is missing in your setter code block for name.
It should be -
set name(newName){
return this._name = newName;
},

Not so fast. Setters don’t need to return anything. Like the name says, they SET things, instance properties.

3 Likes

I could not pass part 2, writing the getters and setters, even though my code matched the answer exactly except for putting all of the getters before all of the setters.

2 Likes

I could use a hint as well for the third task:

Add two methods to your object: .bark() which returns ‘ruff! ruff!’ and .eatTooManyTreats() which should increment the weight property by 1.

I’ve tried both

eatTooManyTreats() {
      this.weight = this.weight+1;
}

and

eatTooManyTreats() {
      this.weight++;
}

Neither did the trick and I am not getting any feedback or errors.

Could we please see the entire object?

// Write your code here:
const dogFactory = (_name,_breed,_weight) => {
  return {
    _name,
    _breed,
    _weight,
    get name() {
      return this._name;
    },
    get breed() {
      return this._breed;
    },
    get weight() {
      return this._weight;
    },
    set name(name) {
      (typeof(name)=='string') ? this._name = name : console.log('Please enter a proper string for the name value.');
    },
    set breed(breed) {
      (typeof(breed)=='string') ? (this._breed = breed) : (console.log('Please enter a proper string for the breed value.'));
    },
    set weight(weight) {
      (typeof(weight)=='number' && number>0) ? this._weight = weight : console.log('Please enter a proper number for the weight value.');
    },
    bark() {
      return 'ruff! ruff!';
    },
    eatTooManyTreats() {
      this.weight = this.weight+1;
    }
  }
}

Arrow function expressions are not the same as regular function expressions. They have no this property. Change the function from arrow to,

dogFactory = function( ... ) {

}

and this will behave as expected.

Changing this.weight++ to this._weight++ did the trick. I just thought the eatTooManyTreats() function would be using the get weight() function here. For some reason it doesn’t.

The arrow function expression has been given by the instructions.

Did you change number to weight in the set weight method?

Just realized that the function is not what has the this property, but the object we are returning, that is why it does work.

I was also misled due to the arrow function in the hint. :frowning: That should be modified.