Freecodecamp: Passing Parameters to a Constructor

Created on 14 Nov 2015  路  7Comments  路  Source: freeCodeCamp/freeCodeCamp

Challenge Waypoint: Make Unique Objects by Passing Parameters to our Constructor has an issue.
User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0.
Please describe how to reproduce this issue, and include links to screenshots if possible.

My code:

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

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

// Only change code above this line

(function() {return JSON.stringify(myCar);})();

I only modified the code within the // parameters and no matter the attempt I would get errors. I tried to pass parameters individually and would still receive errors. After asking for help on chat I was able to pass it, but not within the parameters set. I used multiple variations given, but none of them worked within the instructions. The constructor doesn't appear to be able to be modified as is, the other people stated the same thing. Maybe the instructions weren't as clear or this could be a bug. Error stated
"Calling new Car(3,1,2) should produce an object with a wheels property of 3, a seats property of 1, and an engines property of 2."

Most helpful comment

I don't see what the problem is. The instructions are very clear

Alter the Car constructor to use parameters to assign values to the wheels, seats, and engines properties.
Then call your new constructor with three number arguments and assign it to myCar to see it in action.

You need to change the code from

var Car = function() {
  this.wheels = 4;
  this.seats = 1;
  this.engines = 1;
};

to

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

and then assign it to var myCar with the constructor.

All 7 comments

I don't see what the problem is. The instructions are very clear

Alter the Car constructor to use parameters to assign values to the wheels, seats, and engines properties.
Then call your new constructor with three number arguments and assign it to myCar to see it in action.

You need to change the code from

var Car = function() {
  this.wheels = 4;
  this.seats = 1;
  this.engines = 1;
};

to

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

and then assign it to var myCar with the constructor.

Thanks @faizaanceg.

@nasainlee - In the future I encourage you to ask for help in the Help Room on Gitter. There are many helpful folks who can answer your questions there.

:+1:

thanks

Just follow the steps.
my code look like this:

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(4, 5, 1);

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

var myCar = new Car(1,2,3);

chat room doesn't help a lot!!

Was this page helpful?
0 / 5 - 0 ratings