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

抽奖功能无法运行:JavaScript代码问题排查求助

Fixing Your Broken Raffle JavaScript Code

Alright, let's dig into why your raffle code isn't working and fix it up. There are a handful of critical issues here that are causing everything to break:

  • Infinite Loop Chaos
    Your nested do-while and for loops are completely misconfigured. The do-while increments i every iteration, but the for loop's condition checks i<=names.length and never updates i or has a proper termination condition. This will lock up your browser in an infinite loop before it ever gets to the raffle logic.

  • Array Index Out-of-Bounds
    Inside the for loop, you start with count=0, immediately run count++, then access names[count]. Since arrays are 0-indexed, when count hits names.length (3 in your example), names[3] is undefined—that's an invalid index.

  • Broken Random Number Generation
    The GetRandomInt() function uses Math.floor(Math.random()*i+1), which is wrong on two counts:

    1. i ends up being names.length + 1 after your loop runs, so you're generating numbers outside the array's valid index range.
    2. You're starting at 1, which skips the first element in the array (index 0) entirely.
  • Case-Sensitive Variable Mistake
    In CallWinner(), you define var ID = GetRandomInt(); but then reference id (lowercase) later. JavaScript is case-sensitive, so id is treated as an undefined variable here.

  • Messy document.write Usage
    Your original loop spams duplicate output, and if CallWinner() runs after the page loads, document.write will overwrite the entire page—hardly ideal for a raffle display.


Fixed Code

Here's a cleaned-up, working version with clear logic:

// Raffle participant list
const names = ["Stefon", "Garret", "Brandon"];

// Generate a valid array index (0 to names.length - 1)
function getRandomInt() {
  return Math.floor(Math.random() * names.length);
}

// Assign and display raffle IDs (1-based for user-friendliness)
function assignRaffleIds() {
  names.forEach((name, index) => {
    const raffleId = index + 1;
    document.write(`${name} has been assigned to the raffle ID, ${raffleId}<br>`);
  });
}

// Pick and announce the winner
function callWinner() {
  const winnerIndex = getRandomInt();
  const winnerName = names[winnerIndex];
  const winnerId = winnerIndex + 1; // Match the 1-based ID we displayed earlier
  document.write(`<hr>${winnerName} has won with the ID of ${winnerId}!`);
}

// Run the workflow: assign IDs first, then draw the winner
assignRaffleIds();
callWinner();

Key Improvements:

  • Replaced the chaotic nested loops with forEach for clean, readable ID assignment.
  • Fixed the random number generator to produce valid array indexes every time.
  • Corrected the variable case mismatch to avoid undefined errors.
  • Split functionality into separate, single-purpose functions for better maintainability.
  • Ensured no infinite loops and valid array access at all times.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:16:34