我有三個我期望的輸出(第二個測試)中的一個,但無法弄清楚如何得到另外兩個。我也包含下面的測試代碼。我是否需要添加屬性才能生成所需的輸出?
function car(gas, mpg) {
this.gas = gas;
if (this.gas <= 0) {
this.empty = true;
}
this.mpg = mpg;
//this.range = gas * mpg;
//this.empty = empty;
this.drive = function(miles) {
this.gas -= miles/this.mpg;
if (this.gas <= 0) {
this.empty = true;
}
};
this.fill = function(gallons) {
if (this.gas <= 0) {
this.fill = false;
}
};
}
//test code
var test = new car(15, 30);
console.log(test.empty); //expected output = false
test.drive(500);
console.log(test.empty); //expected output = true
test.fill(20);
console.log(test.empty); //expected output = false
該代碼中沒有數組。 –
我已將您的代碼移至* runnable * Stack Snippet,並將'console.log'替換爲'alert'。 –