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

JavaScript数组对象遍历与过滤调试求助——IT训练营作业问题

Troubleshooting Your Car Array Loops & Completing All Tasks

Alright, let's break down what's going wrong with your initial code, then walk through all 5 tasks using only basic loops (no array methods like filter() as you requested).

First, the bugs in your original code:

  1. Array Indexing Mistake: JavaScript arrays are 0-indexed—your loop starts at i=1, which means you're skipping the first BMW car entirely. You need to start at i=0.
  2. Typo: You wrote cars.lenght instead of cars.length (missing the 't'). Since lenght isn't a valid property, your loop condition i <= cars.lenght evaluates to false immediately, so the loop never runs. That's why you got an empty [] in the console.

Let's fix that first task, then tackle all the requirements one by one:


Task 1: Filter & print all cars with price under $60,000

let cars = [
  { Brand: "BMW", price: 70000, horsePower: 200, color: "white", yearOfConstruction: 2020 },
  { Brand: "Mazda", price: 45000, horsePower: 220, color: "silver", yearOfConstruction: 2021 },
  { Brand: "Volvo", price: 55000, horsePower: 190, color: "beige", yearOfConstruction: 2021 },
  { Brand: "Opel", price: 38000, horsePower: 155, color: "black", yearOfConstruction: 2021 },
  { Brand: "Mazda", price: 22000, horsePower: 90, color: "magenta", yearOfConstruction: 2021 },
  { Brand: "Fiat", price: 19000, horsePower: 110, color: "blue", yearOfConstruction: 2019 }
];

// Task 1: Print all cars with price < 60000
console.log("=== Task 1: Cars Under $60,000 ===");
for (let i = 0; i < cars.length; i++) {
  if (cars[i].price < 60000) {
    // Print every property clearly
    console.log(`
Brand: ${cars[i].Brand}
Price: $${cars[i].price}
Horsepower: ${cars[i].horsePower}hp
Color: ${cars[i].color}
Year Built: ${cars[i].yearOfConstruction}
---`);
  }
}

Task 2: Filter cars with horsepower over 150hp

console.log("\n=== Task 2: Cars With >150 Horsepower ===");
for (let i = 0; i < cars.length; i++) {
  if (cars[i].horsePower > 150) {
    console.log(`${cars[i].Brand} - ${cars[i].horsePower}hp`);
    // If you want full property details, use the same format as Task 1
  }
}

Task 3: Print cars built after 2020 AND with horsepower under 100hp

console.log("\n=== Task 3: Post-2020 Cars With <100 Horsepower ===");
for (let i = 0; i < cars.length; i++) {
  // "2020年后生产" means year > 2020 (2020 itself isn't "after" 2020)
  if (cars[i].yearOfConstruction > 2020 && cars[i].horsePower < 100) {
    console.log(cars[i]); // Prints the full car object
  }
}

Task 4: Print cars that are Fiat OR have horsepower under 150hp

console.log("\n=== Task 4: Fiat Cars OR <150 Horsepower ===");
for (let i = 0; i < cars.length; i++) {
  if (cars[i].Brand === "Fiat" || cars[i].horsePower < 150) {
    console.log(cars[i]);
  }
}

All of these use only standard for loops, no fancy array methods. The key takeaways here are always double-checking array indexing (start at 0!) and property spelling—small typos can break your entire loop.

内容的提问来源于stack exchange,提问作者B. N. B

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.27 14:04:05