Problems with My Constructor Function


#1

Does anyone know what I am doing wrong?

If you invoke the Car constructor without passing in a value for the year it should default to the current year. The checkMilleage property should be set to false by default.

// Create a Car constructor function that has make, model, year, color and checkOil properties. 

function  Car (make, model, year, color, checkOil) {

this.make = make;

this.model = model;

this.year = year;

this.color = color;

this.checkOil = checkOil;


}
// Add a blowHorn method to the Car constructor's prototype that will log "Honk! Honk!" To the console when called. 

this.blowHorn = function(){
console.log("Honk! Honk!");
};

// Create another method on the Car prototype called updateMileage that when called will increment the car's mileage by a given amount of miles. 

this.car.carMileage = function () {
 carMileage++;
};

// Create another method called checkOilLight that will set the checkOil property to true if the car has been driven 1,500 miles. 

this.checkOilLight = function() {
if(checkOil = 1500){
return checkOil = true;
}
};

var sentra = new Car ("nissan", "sentra", 2004, "red", "ok");

Are My Methods Correct?
Is My Method Correct?
#2

Im not going to give you the anwser but... Let me give you 2 hints.. First the functions are suppose to be Inside the constructor. And secondly thier suppose to be seperated by commas...

Also please take a look at this:


#4

do methods also go inside functions? Ie.

// Add a blowHorn method to the Car constructor's prototype that will log "Honk! Honk!" To the console when called.

this.blowHorn = function(){
console.log("Honk! Honk!");
};


#5

I made some corrections. I think this may be correct.

// Create a Car constructor function that has make, model, year, color and checkOil properties.
function Car (make, model, year, color) {

var dateObj = new Date();

//The checkOil property should be set to false by default.
this.checkOil = false;

this.make = make;

this.model = model;

this.year = year;

this.color = color;

//If you invoke the Car constructor without passing in a value for the year it should default to the current year.
if (year !=== undefined) {
this.year = year;
} else{
this.year = date.getFullYear();
}

// Create another method called checkOilLight that will set the checkOil property to true if the car has been driven 1,500 miles.

this.checkOilLight = function() {
if(checkOil === 1500){
return checkOil = true;
}

// Add a blowHorn method to the Car constructor's prototype that will log "Honk! Honk!" To the console when called.

this.blowHorn = function(){
console.log("Honk! Honk!");
}

// Create another method on the Car prototype called updateMileage that when called will increment the car's mileage by a given amount of miles.

this.car.updateMileage = function () {
carMileage++;
}

var sentra = new Car ("nissan", "sentra", 2004, "red", "ok");


#6

You forgot to close your car function..


#7

My update

// Create a Car constructor function that has make, model, year, color and checkOil properties.
function Car (make, model, year, color) {

var dateObj = new Date();

//The checkOil property should be set to false by default.
this.checkOil = false;

this.make = make;

this.model = model;

this.year = year;

this.color = color;

//If you invoke the Car constructor without passing in a value for the year it should default to the current year.
if (year !=== undefined) {
this.year = year;
} else{
this.year = date.getFullYear();
}

// Create another method called checkOilLight that will set the checkOil property to true if the car has been driven 1,500 miles.

Car.prototype.checkOilLight = function() {
if(this.miles >= 1500){
this.checkOil = true;
}

// Add a blowHorn method to the Car constructor's prototype that will log "Honk! Honk!" To the console when called.

Car.prototype.blowHorn = function(){
console.log("Honk! Honk!");
}

// Create another method on the Car prototype called updateMileage that when called will increment the car's mileage by a given amount of miles.

Car.prototype.updateMileage = function (milesDriven) {
this.miles++ += milesDriven;

}

var sentra = new Car ("nissan", "sentra", 2004, "red", "ok");