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

JavaScript对象无法在if语句中正常工作的问题求助

Troubleshooting Your ShipTypes Object & If Statements for Your Text Game

Hey there! As someone who’s been right where you are—fumbling through JS logic as a newbie—let’s work through why your ShipTypes object and if statements aren’t playing nice. Most of the time, this boils down to either how you’re structuring the ShipTypes object or how you’re checking against it in your conditions. Let’s break this down step by step with actionable examples.

First, Fix the ShipTypes Structure

It’s easy to mix up how you organize this object—here are two clear, usable setups depending on your game’s needs:

Option 1: ShipTypes as Allowed/Restricted Arrays

If you want to group ships into allowed and restricted lists, define ShipTypes like this:

// Organized object with arrays for allowed/restricted vessel types
const ShipTypes = {
  allowed: ["Fishing Boat", "Cargo Ship", "Passenger Ferry"],
  restricted: ["Military Destroyer", "Pirate Galleon"]
};

Option 2: ShipTypes as a Lookup Object (Ship Name → Permission Status)

If you want a direct map of ship names to whether they’re allowed, use key-value pairs:

// Key = ship name, Value = boolean (allowed = true, restricted = false)
const ShipTypes = {
  "Fishing Boat": true,
  "Cargo Ship": true,
  "Military Destroyer": false,
  "Pirate Galleon": false
};

Next, Fix Your If Statement Logic

Now let’s pair these structures with working if statements, plus fixes for common pitfalls like case sensitivity:

For the Array-Based ShipTypes

Use includes() to check if the user’s input exists in your allowed/restricted lists. We’ll also normalize input to lowercase to avoid false negatives from capitalization differences:

const userShipInput = prompt("Please enter your ship's name:").trim().toLowerCase();

// Normalize our ship lists to match input case
const allowedShips = ShipTypes.allowed.map(ship => ship.toLowerCase());
const restrictedShips = ShipTypes.restricted.map(ship => ship.toLowerCase());

if (allowedShips.includes(userShipInput)) {
  alert("Welcome aboard! Your ship is permitted to dock.");
} else if (restrictedShips.includes(userShipInput)) {
  alert("Sorry, this port doesn't allow your ship type.");
} else {
  alert("We don't recognize that ship name. Please try again!");
}

For the Lookup Object-Based ShipTypes

Use hasOwnProperty() to check if the ship exists in your object, then verify its permission status:

const userShipInput = prompt("Enter your ship's name:").trim();

if (ShipTypes.hasOwnProperty(userShipInput)) {
  if (ShipTypes[userShipInput]) {
    alert("Permission granted! Feel free to dock.");
  } else {
    alert("Access denied—your ship type is restricted here.");
  }
} else {
  alert("Unknown ship name. Please double-check your input.");
}

Common Mistakes to Watch For

  • Case Sensitivity: If your user types "fishing boat" but your ShipTypes uses "Fishing Boat", the check will fail—normalizing to lowercase fixes this.
  • Incorrect Object Access: Don’t try comparing the user’s input directly to the entire ShipTypes object (e.g., if (userInput === ShipTypes)—this will never be true!). Always target the specific property or array inside the object.
  • Typos: Double-check that ship names in your ShipTypes match exactly what you expect users to input (or add normalization if you want to account for small typos).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 08:26:55