Here is my java script code.
var fiat = {
make: "Fiat",
model: "500",
year: 1957,
color: "Medium Blue",
passengers: 2,
convertible: false,
mileage: 88000,
fuel: 0,
started: false,
start: function() {
if (this.fuel == 0) {
console.log("The car is on empty, fill up before starting!");
} else {
this.started = true;
}
},
stop: function() {
this.started = false;
},
drive: function() {
function update(){
this.fuel-=-1;
}
if (this.started) {
if (this.fuel > 0) {
console.log(this.make + " " +
this.model + " goes zoom zoom!");
update();
} else {
console.log("Uh oh, out of fuel.");
this.stop();
}
} else {
console.log("You need to start the engine first.");
}
},
addFuel: function(amount) {
this.fuel = this.fuel + amount;
}
};
I want to update the fuel by invoking the helper function "update()" nested inside the property function "drive". I checked in the console it seems I can't access the variables this.fuel property since it prints "NaN".
The question is How do I access the objects property from the "update()" helper nested inside the "drive" property function so that I can make changes to "this.fuel". Thanks.