Bubble sort program not passing

hi, totally blind. using the jaws 2026 screen reader. using windows 11 pro. using microsoft edge. now got a issue with the lab. the dom for the generate button is loading properly and confusing the fcc editor. have tried researching, tried different approaches, and then did try to research. and it is not passing, so can any one help me out like maybe ray. so i will paste the html, javascript and the errors list below. tried multiple reset the lesson, multiple refreshes, and then so have tried to get it working. so need some guidance.
thank you.
marvin.
ps: pasting below:

html:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Sorting Visualizer</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>
    <main>
        <div id="array-container">
            <div id="starting-array"></div>
        </div>
        <div id="btn-container">
            <button id="generate-btn" type="button">Generate Array</button>
            <button id="sort-btn" type="button">Sort Array</button>
        </div>
    </main>
    <script src="script.js"></script>
</body>

</html>

java script:

// Generate a random integer between 1 and 100 inclusive
function generateElement() {
  return Math.floor(Math.random() * 100) + 1;
}

// Generate an array of five random integers
function generateArray() {
  const arr = [];
  for (let i = 0; i < 5; i++) {
    arr.push(generateElement());
  }
  return arr;
}

// Generate an empty div container
function generateContainer() {
  return document.createElement("div");
}

// Fill a container element with span elements showing array values
function fillArrContainer(container, array) {
  container.innerHTML = ""; // Clear existing content
  array.forEach((num) => {
    const span = document.createElement("span");
    span.textContent = num;
    container.appendChild(span);
  });
}

// Check if first number is less than or equal to second
function isOrdered(a, b) {
  return a <= b;
}

// Swap elements at index and index+1 if out of order
function swapElements(array, index) {
  if (!isOrdered(array[index], array[index + 1])) {
    const temp = array[index];
    array[index] = array[index + 1];
    array[index + 1] = temp;
  }
}

// Highlight two elements in a container at index and index+1
function highlightCurrentEls(container, index) {
  const children = container.children;
  for (let i = 0; i < children.length; i++) {
    children[i].style.border = "none"; // Remove previous highlights
  }
  if (children[index] && children[index + 1]) {
    children[index].style.border = "2px dashed red";
    children[index + 1].style.border = "2px dashed red";
  }
}

// Generate button behavior
document.getElementById("generate-btn").addEventListener("click", () => {
  const startingArrayEl = document.getElementById("starting-array");
  const arrayContainer = document.getElementById("array-container");

  // Remove all other divs except starting-array
  Array.from(arrayContainer.children).forEach((child) => {
    if (child.id !== "starting-array") arrayContainer.removeChild(child);
  });

  const arr = generateArray();
  fillArrContainer(startingArrayEl, arr);

  // Highlight first two numbers
  highlightCurrentEls(startingArrayEl, 0);
});

// Sort button behavior
document.getElementById("sort-btn").addEventListener("click", () => {
  const startingArrayEl = document.getElementById("starting-array");
  const arrayContainer = document.getElementById("array-container");
  const originalArray = Array.from(startingArrayEl.children).map(
    (span) => Number(span.textContent)
  );

  let steps = [];
  let arr = [...originalArray];
  steps.push([...arr]);

  // Bubble sort logic with steps
  for (let i = 0; i < arr.length - 1; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      swapElements(arr, j);
      steps.push([...arr]);
    }
  }

  // Clear any previous steps except starting-array
  Array.from(arrayContainer.children).forEach((child) => {
    if (child.id !== "starting-array") arrayContainer.removeChild(child);
  });

  // Create divs for each step
  steps.forEach((step) => {
    const div = generateContainer();
    fillArrContainer(div, step);
    arrayContainer.appendChild(div);
  });

  // Highlight first two elements in starting-array
  highlightCurrentEls(startingArrayEl, 0);
});

error list:

  • Passed:1. You should have a function named generateElement.

  • Passed:2. Your generateElement function should return a random integer between 1 and 100 inclusive.

  • Passed:3. You should have a function named generateArray.

  • Passed:4. Your generateArray function should make use of the generateElement function.

  • Passed:5. Your generateArray function should return an array containing five random integers between 1 and 100.

  • Passed:6. You should have a function named generateContainer.

  • Passed:7. Your generateContainer function should return an empty div element.

  • Passed:8. You should have a function named fillArrContainer.

  • Passed:9. Your fillArrContainer() should take an element as its first parameter and an array of integers as its second parameter, then populate the element with five span elements, with each span showing one of the integers from the array.

  • Passed:10. You should have a function named isOrdered.

  • Passed:11. Your isOrdered function should take two integers and should return a boolean indicating if the first integer is less than or equal to the second.

  • Passed:12. You should have a function named swapElements.

  • Passed:13. Your swapElements function take an array of integers and a numeric index as arguments. It should modify the array passed in place by swapping the element at the given index and the following element if the first element is greater than the second.

  • Passed:14. You should have a function named highlightCurrentEls.

  • Passed:15. Your highlightCurrentEls function should give the descendants of the specified element, located at the given index and the next index, a border that is dashed, red, and set to a width of your choice.

  • Passed:16. When you click #generate-btn you should fill #starting-array with five span elements, each with a random number between 1 and 100 as its text.

  • Passed:17. When #starting-array already contains a generated array, or #array-container contains the sorted array, clicking the #generate-btn should remove other elements in the #array-container, leaving only #starting-array with newly generated numbers.

  • Failed:18. After you click #sort-btn, #array-container should contain as many div elements as the steps required by the Bubble Sort algorithm to sort the starting array, including the div representing the starting array and a div representing the sorted array.

  • Failed:19. After you click #sort-btn, each div within #array-container should contain five span, each with a number as its text, and arranged to represent the steps required by Bubble Sort algorithm to sort the starting array.

  • Passed:20. When you click the #sort-btn, you should make use of the highlightCurrentEls function to highlight the elements being compared in each step.

  • Passed:21. After you click #sort-btn, #starting-array should represent the starting step with the initial array and the first two integers highlighted using highlightCurrentEls.

Please do not request specific team members.

Please post a link to the lab.

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Get Help > Ask for Help button located on the challenge.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Please review the User Stories and make sure each one is implemented as described.

You can start with User Story 11:

  1. When you click #generate-btn you should use the fillArrContainer function to fill #starting-array with five span elements, each with a random number as its text. If present, other elements should be removed from #array-container.

The generate button should not highlight any numbers when clicked.

In this case, it helps if you talk about why you think your code should satisfy the tests and user stories you are failing.

hi, okay tried to then code and then research. only two errors and cannot seem to figure out how to get this so to pass. totally blind and using a screen reader. so is it my code, hidden characters or something else. will paste my current html, js and the errors. can you tell me how to get this to pass. what am i missing. and cannot see any hidden characters, hidden spacing as totally blind. and can you help me get this to pass. took me a few hours, then looking up how to do this code from a example on the web, and then did try to then start coding, but got stuck and so looked at an example how to do this. so what am i missing. thank you.
marvin.
ps: pasting the html and js and the errors below.

html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sorting Visualizer</title>
  <style>
    span { margin: 0 5px; padding: 2px; }
  </style>
</head>
<body>
  <div id="starting-array"></div>
  <div id="array-container"></div>
  <button id="generate-btn">Generate Array</button>
  <button id="sort-btn">Sort Array</button>

  <script src="script.js"></script>
</body>
</html>

java script:

// 1. Returns a random integer between 1 and 100
function generateElement() {
  return Math.floor(Math.random() * 100) + 1;
}

// 2. Returns an array of 5 random integers
function generateArray() {
  const arr = [];
  for (let i = 0; i < 5; i++) arr.push(generateElement());
  return arr;
}

// 3. Returns an empty <div>
function generateContainer() {
  return document.createElement("div");
}

// 4. Fills a container with five spans
function fillArrContainer(container, arr) {
  container.innerHTML = "";
  arr.forEach(num => {
    const span = document.createElement("span");
    span.textContent = num;
    container.appendChild(span);
  });
}

// 5. Returns true if a <= b
function isOrdered(a, b) {
  return a <= b;
}

// 6. Swaps elements if out of order
function swapElements(arr, idx) {
  if (!isOrdered(arr[idx], arr[idx + 1])) {
    const temp = arr[idx];
    arr[idx] = arr[idx + 1];
    arr[idx + 1] = temp;
  }
}

// 7. Highlights two elements
function highlightCurrentEls(container, idx) {
  const spans = container.querySelectorAll("span");
  spans.forEach(s => s.style.border = "");
  if (spans[idx]) spans[idx].style.border = "2px dashed red";
  if (spans[idx + 1]) spans[idx + 1].style.border = "2px dashed red";
}

// DOM elements
const generateBtn = document.getElementById("generate-btn");
const sortBtn = document.getElementById("sort-btn");
const startingArrayDiv = document.getElementById("starting-array");
const arrayContainer = document.getElementById("array-container");

let currentArray = [];

// Generate button
generateBtn.addEventListener("click", () => {
  currentArray = generateArray();

  // Clear array container
  arrayContainer.innerHTML = "";

  // Fill starting array
  fillArrContainer(startingArrayDiv, currentArray);
  highlightCurrentEls(startingArrayDiv, 0);
});

// Sort button
sortBtn.addEventListener("click", () => {
  if (!currentArray.length) return;

  arrayContainer.innerHTML = ""; // Clear previous steps

  const arr = [...currentArray];
  const n = arr.length;

  // First step: show starting array in array-container
  const firstDiv = generateContainer();
  fillArrContainer(firstDiv, arr);
  highlightCurrentEls(firstDiv, 0);
  arrayContainer.appendChild(firstDiv);

  // Bubble Sort visualization: every comparison
  for (let i = 0; i < n - 1; i++) {
    for (let j = 0; j < n - 1 - i; j++) {
      // Step before swap
      const stepDiv = generateContainer();
      fillArrContainer(stepDiv, arr);
      highlightCurrentEls(stepDiv, j);
      arrayContainer.appendChild(stepDiv);

      // Swap if needed
      swapElements(arr, j);

      // Step after swap (optional for visualization)
      const afterDiv = generateContainer();
      fillArrContainer(afterDiv, arr);
      highlightCurrentEls(afterDiv, j);
      arrayContainer.appendChild(afterDiv);
    }
  }

  // Final sorted array step
  const sortedDiv = generateContainer();
  fillArrContainer(sortedDiv, arr);
  arrayContainer.appendChild(sortedDiv);
});

erorrs:

  • Passed:1. You should have a function named generateElement.

  • Passed:2. Your generateElement function should return a random integer between 1 and 100 inclusive.

  • Passed:3. You should have a function named generateArray.

  • Passed:4. Your generateArray function should make use of the generateElement function.

  • Passed:5. Your generateArray function should return an array containing five random integers between 1 and 100.

  • Passed:6. You should have a function named generateContainer.

  • Passed:7. Your generateContainer function should return an empty div element.

  • Passed:8. You should have a function named fillArrContainer.

  • Passed:9. Your fillArrContainer() should take an element as its first parameter and an array of integers as its second parameter, then populate the element with five span elements, with each span showing one of the integers from the array.

  • Passed:10. You should have a function named isOrdered.

  • Passed:11. Your isOrdered function should take two integers and should return a boolean indicating if the first integer is less than or equal to the second.

  • Passed:12. You should have a function named swapElements.

  • Passed:13. Your swapElements function take an array of integers and a numeric index as arguments. It should modify the array passed in place by swapping the element at the given index and the following element if the first element is greater than the second.

  • Passed:14. You should have a function named highlightCurrentEls.

  • Passed:15. Your highlightCurrentEls function should give the descendants of the specified element, located at the given index and the next index, a border that is dashed, red, and set to a width of your choice.

  • Passed:16. When you click #generate-btn you should fill #starting-array with five span elements, each with a random number between 1 and 100 as its text.

  • Failed:17. When #starting-array already contains a generated array, or #array-container contains the sorted array, clicking the #generate-btn should remove other elements in the #array-container, leaving only #starting-array with newly generated numbers.

  • Failed:18. After you click #sort-btn, #array-container should contain as many div elements as the steps required by the Bubble Sort algorithm to sort the starting array, including the div representing the starting array and a div representing the sorted array.

  • Failed:19. After you click #sort-btn, each div within #array-container should contain five span, each with a number as its text, and arranged to represent the steps required by Bubble Sort algorithm to sort the starting array.

  • Passed:20. When you click the #sort-btn, you should make use of the highlightCurrentEls function to highlight the elements being compared in each step.

  • Passed:21. After you click #sort-btn, #starting-array should represent the starting step with the initial array and the first two integers highlighted

, and then to

You didn’t say, which piece of your code do you think should satisfy your failing tests?

hi, steps 17,18 and 19 are failing. so did try to google to try to find a solution. but could not. so can you help me out and dont be sarcastic. can you help? if not, then maybe another person who is not so turse, can help. totally blind and trying to learn. try using a laptop and close your eyes, and use a text to speech, and did try f 1 for the editor, did not work. so the fcc editor not built with accessbiility with the ground up. just an after thought. go figure.

The generate button is still highlighting the first two elements of the array and it shouldn’t.

Can you just identify that line of code?

We know you are blind. You don’t need to repeat it in every post.

Can you please try answering this question:

Forum rules prevent anyone from writing the answer for you, so I am trying to ask you questions that will help you understand how to fix the code. But that only works if you try to answer the questions.

hi, okay error 17 should then loop through my array in the sort. but it is not working. erorr 18 is when the numbers being sorted and swapped are not swapping correctly. error 19 is when the array is not providing the correct numbers and sorting, so did try to research this to understand and did try researching. so can you help me out and this is what the user stories supposed to be in plain english. thanks for trying to help. i know i am only one blind developer student, and you have more inmportant sighted people to help, i get that. so, need your help. thanks.

Focus on one test at a time.

Failed:17. When #starting-array already contains a generated array, or #array-container contains the sorted array, clicking the #generate-btn should remove other elements in the #array-container, leaving only #starting-array with newly generated numbers.

What block of code implements this? Please share it here.

Multiple people are here replying to your messages and helping you, so please knock it off with this nonsense?

I know what the tests are, but can you please say which specific parts of your code you think should meet these tests?

hi, heres the function.
marvin:

document.getElementById('generate-btn').addEventListener('click', function() {
  const arrayContainer = document.getElementById('array-container');
  const startingArray = document.getElementById('starting-array');

  // Remove all previous children in the array container except starting-array
  Array.from(arrayContainer.children).forEach(child => {
    if (child.id !== 'starting-array') {
      arrayContainer.removeChild(child);
    }
  });

  // Clear the starting-array content
  startingArray.innerHTML = '';

  // Generate new random numbers and add to starting-array
  const newArray = [];
  for (let i = 0; i < 10; i++) { // or however many elements you need
    const randomNum = Math.floor(Math.random() * 100) + 1;
    newArray.push(randomNum);

    const item = document.createElement('div');
    item.className = 'array-item';
    item.textContent = randomNum;
    startingArray.appendChild(item);
  }
});

Why is this hardcoded to 10?

hi, i dont know. sorry, was looking at an example. and not sure why? stupid me. sorry.

You don’t need to insult yourself.

But it is important to have a reason for each piece of code you write. How many elements does the instructions say to use?

hi, 5 i think that the insturctions say..

Then your loop needs to add as many elements as the instructions are looking for, so that the tests can find what they expect.

hi, okay stuck on step 18 which is the div have one div and then multiple divs for the bubble sort, step 19 is the number of 5 span divs for each sort, but it is not passing, so what am i doing wrong. trying to learn. the fcc editor is not the most friendliest online editor to use, not able to do control f for find, try that then put in the phrase but does not go to the correct place, either goes to the previous line or the next line or even pagraph or block of code, then have to scroll up or down. so it could be more screen reader friendly. so pasting my html and js. please be gentle. learning and trying to learn and trying to get this to work. so, please no sarcasam and no turse, thanks.
marvin.
ps: pasting below.

html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sorting Visualizer</title>
  <style>
    span { margin: 0 5px; padding: 2px; }
  </style>
</head>
<body>
<div id="array-container">
  <div id="starting-array"></div>
</div>
  
  <button id="generate-btn">Generate Array</button>
  <button id="sort-btn">Sort Array</button>

  <script src="script.js"></script>
</body>
</html>

java script:

// 1. Returns a random integer between 1 and 100
function generateElement() {
  return Math.floor(Math.random() * 100) + 1;
}

// 2. Returns an array of 5 random integers
function generateArray() {
  const arr = [];
  for (let i = 0; i < 5; i++) {
    arr.push(generateElement());
  }
  return arr;
}

// 3. Creates an empty <div>
function generateContainer() {
  return document.createElement("div");
}

// 4. Fills a container with five spans
function fillArrContainer(container, arr) {
  container.innerHTML = "";
  for (let i = 0; i < arr.length; i++) {
    const span = document.createElement("span");
    span.textContent = arr[i];
    container.appendChild(span);
  }
}

// 5. Returns true if a <= b
function isOrdered(a, b) {
  return a <= b;
}

// 6. Swaps elements if out of order (uses isOrdered)
function swapElements(arr, idx) {
  if (!isOrdered(arr[idx], arr[idx + 1])) {
    const tmp = arr[idx];
    arr[idx] = arr[idx + 1];
    arr[idx + 1] = tmp;
  }
}

// 7. Highlights two elements (dashed red border)
function highlightCurrentEls(container, idx) {
  const spans = container.querySelectorAll("span");
  for (let i = 0; i < spans.length; i++) {
    spans[i].style.border = "";
  }
  if (spans[idx]) spans[idx].style.border = "2px dashed red";
  if (spans[idx + 1]) spans[idx + 1].style.border = "2px dashed red";
}

// DOM refs (expects #starting-array inside #array-container)
const generateBtn = document.getElementById("generate-btn");
const sortBtn = document.getElementById("sort-btn");
const startingArrayDiv = document.getElementById("starting-array");
const arrayContainer = document.getElementById("array-container");

let currentArray = [];

// Generate button: produce 5 numbers and keep only #starting-array in container
generateBtn.addEventListener("click", () => {
  currentArray = generateArray();

  // Remove all children except the starting-array element
  while (arrayContainer.children.length > 1) {
    arrayContainer.removeChild(arrayContainer.lastElementChild);
  }

  fillArrContainer(startingArrayDiv, currentArray);
  highlightCurrentEls(startingArrayDiv, 0);
});

// Sort button: produce snapshots after each comparison (swap first, then record)
sortBtn.addEventListener("click", () => {
  if (!currentArray.length) return;

  // Remove all children except the starting-array element
  while (arrayContainer.children.length > 1) {
    arrayContainer.removeChild(arrayContainer.lastElementChild);
  }

  const arr = [...currentArray];
  const n = arr.length;

  // First snapshot: starting array (copy)
  const startDiv = generateContainer();
  fillArrContainer(startDiv, arr);
  highlightCurrentEls(startDiv, 0);
  arrayContainer.appendChild(startDiv);

  // For each comparison: swap if needed, then record snapshot (after comparison)
  for (let i = 0; i < n - 1; i++) {
    for (let j = 0; j < n - 1 - i; j++) {
      swapElements(arr, j);                 // do comparison & swap if needed
      const stepDiv = generateContainer(); // snapshot AFTER that comparison
      fillArrContainer(stepDiv, arr);
      highlightCurrentEls(stepDiv, j);
      arrayContainer.appendChild(stepDiv);
    }
  }
});

erorrs:
Passed:1. You should have a function named generateElement.
Passed:2. Your generateElement function should return a random integer between 1 and 100 inclusive.
Passed:3. You should have a function named generateArray.
Passed:4. Your generateArray function should make use of the generateElement function.
Passed:5. Your generateArray function should return an array containing five random integers between 1 and 100.
Passed:6. You should have a function named generateContainer.
Passed:7. Your generateContainer function should return an empty div element.
Passed:8. You should have a function named fillArrContainer.
Passed:9. Your fillArrContainer() should take an element as its first parameter and an array of integers as its second parameter, then populate the element with five span elements, with each span showing one of the integers from the array.
Passed:10. You should have a function named isOrdered.
Passed:11. Your isOrdered function should take two integers and should return a boolean indicating if the first integer is less than or equal to the second.
Passed:12. You should have a function named swapElements.
Passed:13. Your swapElements function take an array of integers and a numeric index as arguments. It should modify the array passed in place by swapping the element at the given index and the following element if the first element is greater than the second.
Passed:14. You should have a function named highlightCurrentEls.
Passed:15. Your highlightCurrentEls function should give the descendants of the specified element, located at the given index and the next index, a border that is dashed, red, and set to a width of your choice.
Passed:16. When you click #generate-btn you should fill #starting-array with five span elements, each with a random number between 1 and 100 as its text.
Passed:17. When #starting-array already contains a generated array, or #array-container contains the sorted array, clicking the #generate-btn should remove other elements in the #array-container, leaving only #starting-array with newly generated numbers.
Failed:18. After you click #sort-btn, #array-container should contain as many div elements as the steps required by the Bubble Sort algorithm to sort the starting array, including the div representing the starting array and a div representing the sorted array.
Failed:19. After you click #sort-btn, each div within #array-container should contain five span, each with a number as its text, and arranged to represent the steps required by Bubble Sort algorithm to sort the starting array.
Passed:20. When you click the #sort-btn, you should make use of the highlightCurrentEls function to highlight the elements being compared in each step.
Passed:21. After you click #sort-btn, #starting-array should represent the starting step with the initial array and the first two integers highlighted using highlightCurrentEls.