CODE CHALLENGES: INTERMEDIATE JAVASCRIPT dogFactory()


i had finish the code of dogFactory( )function with this code below :
// Write your code here:
const dogFactory = (name, breed, weight) => {
return {
_name:name,
_breed:breed,
_weight:weight,
get name(){
return this._name
},
set name(newName) {
if(typeof newName ==‘string’) {
return this.name = newName
} else {
return ‘please enter type compatible to name’
}
},
get breed() {
return this._breed
},
set breed (newBreed) {
if(typeof newBreed ==‘string’) {
return this.breed = newBreed
} else {
return ‘please enter type compatible to breed’
}
},
get weight() {
return this._weight
},
set weight(newWeight) {
if(typeof newWeight == ‘number’) {
this.weight = newWeight;
}
}
}
}

const dog = dogFactory(10, ‘staff’, 12)
console.log(dog)
but the console return me this output //{ _name: 10,
_breed: ‘staff’,
_weight: 12,
name: [Getter/Setter],
breed: [Getter/Setter],
weight: [Getter/Setter] }
in despite of make a if/else statement the code still return me number when i assign to the property name a number

There is a tool for formatting code, please use it so your code will be more readable.
CC

you also can edit your topic if you want.

thank you for the information

1 Like
  const dog = dogFactory(10, 'staff', 12)
  console.log(dog) it return in the output//  { _name: 10,
  name: [Getter/Setter],
  _breed: 'staff',
  breed: [Getter/Setter],
  _weight: 12,
  weight: [Getter/Setter] }
type or past// Write your code here:

const dogFactory = (name, breed, weight) => {
 return {
   _name:name,
      get name(){
   return this._name
 },
 set name(newName) {
   if(typeof newName ==='string') {
     this.name = newName
   } 
   else {
       console.log('You must make string to name')
   }
   },
   _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:weight,
  get weight() {
       return this._weight
     },
     set weight(newWeight) {
       if(typeof newWeight === 'number') {
      this.weight = newWeight
     } 
     else {
       console.log ('You must assign anumber to weight')
     }
     }
}
}
  const dog = dogFactory(10, 'staff', 12)
  console.log(dog)
  


This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.