freeCodeCamp Challenge Guide: Extend Constructors to Receive Arguments

Extend Constructors to Receive Arguments


Problem Explanation

Just like in the Bird() example, the Dog() function must takle two parameters - name and color. The name and color must then be initialised within the function using the this keyword. The final property - numLegs is set to 4 as the function doesn’t take in a numLegs parameter.


Solutions

Solution 1 (Click to Show/Hide)
function Dog(name, color) {
  this.name = name;
  this.color = color;
  this.numLegs = 4;
}
let terrier = new Dog("George", "White");
14 Likes

this is what I have, but it’s not working, anybody able to tell me what I’m doing wrong?

var Car = function(wheels, seats, engines) {
//Change this constructor
this.wheels = 4;
this.seats = 5;
this.engines = 1;
};

//Try it out here
var myCar = new Car(3,1,2);

1 Like

var Car = function(wheels, seats, engines) {
//Change this constructor
this.wheels = wheels;
this.seats = seats;
this.engines = engines;
};

//Try it out here
var myCar = new Car(3,1,2);

5 Likes

whats up i had the same problem check out this link to get an answer to this problem https://www.w3schools.com/js/js_object_definition.asp

scroll down to

“Using an Object Constructor”

about midway the page.

pretty much apart from adding them to the parameter we must also do someething like below

this.wheels= wheels

hope this helps

2 Likes

Took me awhile to figure out but make sure to go back a few lessons and hone your skills in creating new objects.

7 Likes