JavaScript: Find the Bug

This JavaScript quiz shows short functions with the kind of bugs that reach code review and production. You will predict the wrong result, name the error and choose the right fix for coercion, this, shallow copies, switch and loop bounds.

ยาก 10 ข้อ ~8 นาที ผ่านที่ 80% Node.js 22

เลือกโหมด

กำลังเล่นแบบผู้เยี่ยมชม: เข้าสู่ระบบเพื่อเก็บ XP และ streak

ตัวอย่างคำถาม

ข้อ 1 จาก 10

The total should be 15. What does this print instead?

function total(prices) {
  let sum = 0;
  for (const p of prices) sum += p;
  return sum;
}
console.log(total(["5", "10"]));
ดูเฉลย

B. 0510 The prices are strings (typical for form inputs or CSV). 0 + "5" is the string "05", and every later += concatenates. Convert first with Number(p).

ควิซนี้มีเนื้อหาอะไรบ้าง

  • String concatenation instead of addition
  • Returning from inside forEach
  • Object identity in includes and ===
  • Losing this when passing a method as a callback
  • Shallow copies, switch fall-through and off-by-one loops

คำถามที่พบบ่อย

ต้องมีบัญชีไหม?

ไม่ต้อง ใครก็เล่นได้ เข้าสู่ระบบเพื่อเก็บ XP, streak, เหรียญตรา และประวัติ

ทำซ้ำได้ไหม?

ได้ ระบบเก็บคะแนนที่ดีที่สุดไว้ และสลับคำถามกับตัวเลือกใหม่ทุกครั้ง