You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JavaScript实现shouldQuarantine原型方法及逻辑bug排查

代码问题修复与功能实现

核心需求

为Wagon构造函数实现shouldQuarantine原型方法,判定规则:乘客列表中存在任意isHealthy属性为false的乘客时,当前马车需要隔离。
现有代码待修复问题:

  • join原型方法乘客登车逻辑错误
  • isHealthy状态更新逻辑不符合规则:乘客进食后食物耗尽时,未正确将isHealthy置为false

原始问题代码

const Traveler = function (travelerName) {
    this.name = travelerName;
    this.food = 1;
    this.isHealthy = true;
};
Traveler.prototype.hunt = function () {
    this.food += 2;
    console.log(this.food);
};
Traveler.prototype.eat = function () {
    this.food -= 1;
    if (this.food === 1) {
    } else {
        this.food === 0;
        this.isHealthy = false;
    }
    console.log(this.food);
};

console.log(new Traveler("John"));

function Wagon(capacity) {
    this.capacity = capacity;
    this.passengers = [];
}
console.log(new Wagon(4));
Wagon.prototype.getAvailableSeatCount = function () {
    let seatingCapacity = this.capacity - this.passengers.length;
    console.log(seatingCapacity);
    return seatingCapacity;
};

Wagon.prototype.join = function (traveler) {
    console.log(this.capacity);
    let currentCapacity = this.capacity;
    if (currentCapacity <= this.passengers.length) {
        this.currentCapacity = 0;
    } else if (this.getAvailableSeatCount != 0) {
        this.passengers.push(traveler);
    }

    console.log(this.passengers);
};
Wagon.prototype.shouldQuarantine = function () {
    for (let i = 0; i < this.passengers.length; i++) {
        if (this.passengers[i].isHealthy) {
            return false;
        }
    }
};

Wagon.prototype.totalFood = function () {
    let totalFood = "";
    this.passengers.forEach(this.food);
    console.log(this.food);
};

逐点修复说明

1. 修复Traveler.prototype.eat健康状态更新逻辑

原方法两处错误:

  • 错用比较运算符===做赋值,this.food === 0仅做值对比,不会修改属性值
  • 判断逻辑错位:只要进食后食物量不等于1,无论剩余多份还是0份,都会标记为不健康,不符合规则
    修复后代码:
Traveler.prototype.eat = function () {
    this.food -= 1;
    // 仅当食物耗尽(小于1)时标记为不健康
    if (this.food < 1) {
        this.isHealthy = false;
    }
    console.log(this.food);
};

2. 修复Wagon.prototype.join登车逻辑

原方法两处错误:

  • 用固定总容量和当前乘客数判断满员,逻辑矛盾无法生效
  • 判断剩余座位时未调用getAvailableSeatCount()方法,直接拿函数引用和0比较,判断条件永远为真,满员后仍可登车
    修复后代码:
Wagon.prototype.join = function (traveler) {
    // 剩余座位数大于0时才允许登车
    if (this.getAvailableSeatCount() > 0) {
        this.passengers.push(traveler);
    }
    console.log(this.passengers);
};

3. 实现Wagon.prototype.shouldQuarantine隔离判定逻辑

原方法逻辑完全写反:遍历到健康乘客就返回false,全量乘客不健康时无返回值(默认undefined),不符合需求。
修复后代码:

Wagon.prototype.shouldQuarantine = function () {
    for (let i = 0; i < this.passengers.length; i++) {
        // 存在任意不健康乘客,直接返回需要隔离
        if (!this.passengers[i].isHealthy) {
            return true;
        }
    }
    // 所有乘客均健康,不需要隔离
    return false;
};

4. 附带修复Wagon.prototype.totalFood统计逻辑

原方法逻辑错误:初始化总食物值为空字符串,forEach传入无效回调,无法正确统计食物总量。
修复后代码:

Wagon.prototype.totalFood = function () {
    let totalFood = 0;
    this.passengers.forEach(passenger => {
        totalFood += passenger.food;
    });
    console.log(totalFood);
    return totalFood;
};

内容的提问来源于stack exchange,提问作者PeteyP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 10:54:16