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

JavaScript冒泡排序:弹窗缺失问题排查求助

Fixing Your Bubble Sort Code: Alert Missing & Negative Number Support

Hey there! Let's break down the two main issues you're facing—the "Fail-Alert missing" test failure and lack of support for negative numbers—along with some small tweaks to get your code fully compliant with the assignment requirements.

Why You're Seeing "Fail-Alert missing"

The biggest culprit here is unhandled edge cases with invalid input:

  • If you enter something like ,,, or non-numeric text, str.match(/\d+/g) returns null. Calling null.map(Number) immediately throws a TypeError, stopping the function cold before it reaches the alert() line.
  • Your bubble sort also has an array out-of-bounds risk: when j hits stop-1, arr[j+1] accesses an undefined value (beyond the array's length), which might cause unexpected behavior in test cases.

Fully Fixed Code

Here's the revised code with all issues addressed, plus comments explaining each change:

<form>
  Enter data to sort : 
  <input type="text" name="mydata" id="mydata" > <!-- Updated ID to match assignment's "mydata" -->
  <br>
  <input type="button" id="databtn" onclick="bubble_sort()" name="databtn" value="Sort Data" /> <!-- Updated ID to match assignment's "databtn" -->
</form>

<script>
function swap(arr, first, second){ 
  var temp = arr[first]; 
  arr[first] = arr[second]; 
  arr[second] = temp; 
}

function bubble_sort() { 
  var str = document.getElementById("mydata").value; // Use the correct ID for the input
  var arr = []; // Initialize empty array to avoid null-related errors
  
  // Capture numbers (including negatives) and convert to number type
  var numberMatches = str.match(/-?\d+/g); // -? allows matching negative numbers
  if (numberMatches) { // Only run map if we found valid numbers
    arr = numberMatches.map(Number);
  }

  // Fixed bubble sort loop to avoid out-of-bounds access
  var len = arr.length;
  for(var i = 0; i < len; i++){ 
    // Inner loop stops at len - i - 1 to prevent accessing arr[j+1] beyond the array
    for(var j = 0; j < len - i - 1; j++){ 
      if(arr[j] > arr[j+1]){ 
        swap(arr, j, j+1); 
      } 
    } 
  } 

  // Ensure alert always fires, even with empty/invalid input
  alert(arr.length > 0 ? arr.join(', ') : "No valid numbers entered");
}
</script>

Key Changes Explained

  1. Fixed Alert Missing Issue:

    • Initialized arr as an empty array upfront, so even if no numbers are found, we never call map on null.
    • Added a check for numberMatches before converting to numbers, preventing TypeError crashes.
    • The final alert handles empty arrays with a friendly message (you can also just use alert(arr) if you prefer, but this makes test behavior clearer).
  2. Added Negative Number Support:

    • Updated the regex from /\d+/g to /-?\d+/g—the -? matches an optional negative sign, so negative numbers are now captured correctly.
  3. Complied with Assignment Requirements:

    • Changed the input's ID to mydata and the button's ID to databtn, matching the exact names specified in your assignment.
  4. Fixed Bubble Sort Edge Case:

    • Adjusted the inner loop condition to j < len - i - 1—this ensures we never try to compare the last element in the unsorted portion to a non-existent arr[j+1].

Test It Out!

Try these scenarios to confirm everything works:

  • Positive numbers: 3,1,4,2 → Alert shows 1, 2, 3, 4
  • Negative numbers: -5,3,-1,0 → Alert shows -5, -1, 0, 3
  • Invalid input: ,,,abc → Alert shows No valid numbers entered
  • Empty input: Click the button without typing anything → Alert shows No valid numbers entered

All these cases will trigger an alert, so you shouldn't see the "Fail-Alert missing" error anymore.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.06 15:02:31