Freecodecamp: Make Instances of Objects with a Constructor Function

Created on 13 Nov 2015  路  9Comments  路  Source: freeCodeCamp/freeCodeCamp

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

// Only change code below this line.

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

// New instance of Car object.
var myCar = new Car();

//Add the property "engines" to myCar, and make it a number.
myCar.engines = 1;

Have tried all solutions and feel I am staring too hard at this to get the final instruction

Then give myCar a nickname property with a string value. I know this should be simple but however I try to call it it will not accept.
Have tried (function() {return JSON.stringify(myCar);})(); at end as others have given in solutions - console.log is not even accepted here, Preventing me moving on.

All 9 comments

Adding a property to an object can be done through dot-notation. Something like the following will satisfy the final instruction:

myCar.nickname = "Bob Ryan";

Thanks and happy coding!

I camel cased nickName like so and gave an error saying I needed to use a string which was very confusing.

The answer is above you @jel111 . Good Luck!!!!!

how is it that changing nickName from camelCase to nickname satisfied the problem?

i'm kinda confused?

You need to use "nickname" (literally. it is written in red). So don't use any property (which is what I did. for e.g. I used .cover or .mirror but that gave me the same string error). They want you to use the word nickname.

they have asked to assign variable as myCar and constructor as Car.this is how it works below
var Car = function() {
this.wheels = 4;
this.engines = 1;
this.seats = 5;
};

// Only change code below this line.
var myCar = new Car();
myCar.nickname = "hourglass23";

Doesn't work for me...doesnt accept solution and console shows nothing.

A common error with this problem is forgetting to put a semicolon after you create a new instance. This can seem a little picky since javascript's automatic semicolon insertion should be taking place here.

// TypeError: "Beast" is not a function - (notice the missing semicolon)
myCar.nickname = "Beast"

// Correct solution with no errors
myCar.nickname = "Beast";

Was this page helpful?
0 / 5 - 0 ratings