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

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?

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.

1 Like

// 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.

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!

‘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.