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.