Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

I’m facing step 18 issue
can anyone help me to resolve the issue

// running tests
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.
// tests completed

Your code so far

<!-- file: index.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>
/* file: styles.css */
* {
    box-sizing: border-box;
}

main {
    height: 100vh;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
}

#array-container {
    max-height: 95vh;
    display: flex;
    flex-direction: column;
    flex-wrap: wrap;
    gap: 2px;

}

#array-container>div {
    min-width: 8rem;
    height: 2rem;
    box-shadow: rgba(50, 50, 93, 0.25) 0px 2px 5px -1px, rgba(0, 0, 0, 0.3) 0px 1px 3px -1px;
    border-radius: 10px;
    margin-bottom: 0.2rem;
    border: 2px solid darkgray;
    display: flex;
    justify-content: space-evenly;
    align-items: center;
}

#starting-array {
    border: 4px solid darkblue !important;
}

#btn-container {
    display: flex;
    justify-content: space-around;
}

button {
    padding: 2px;
    margin: 5px;
}

span {
    border-radius: 2px;
    padding: 0.5px;
    margin: 0
}

@media (min-width: 430px) {
  #array-container>div {
    min-width: 12rem;    
  }
  span {
    padding: 1px;
    margin: 1px;
  }
}
/* file: script.js */
let currentArray = [];

function generateElement() {
  return Math.floor(Math.random() * 100) + 1;
}

function generateArray() {
  const arr = [];
  for (let i = 0; i < 5; i++) {
    arr.push(generateElement());
  }
  return arr;
}

function generateContainer() {
  return document.createElement("div");
}

function fillArrContainer(container, arr) {
  container.innerHTML = ""; // Clear existing content
  arr.forEach(num => {
    const span = document.createElement("span");
    span.textContent = num;
    container.appendChild(span);
  });
}

function isOrdered(a, b) {
  return a <= b;
}

function swapElements(arr, index) {
  if (!isOrdered(arr[index], arr[index + 1])) {
    const temp = arr[index];
    arr[index] = arr[index + 1];
    arr[index + 1] = temp;
  }
}

function highlightCurrentEls(container, index) {
  const children = container.children;
  if (index < children.length) {
    children[index].style.border = "2px dashed red";
  }
  if (index + 1 < children.length) {
    children[index + 1].style.border = "2px dashed red";
  }
}

document.getElementById("generate-btn").addEventListener("click", function () {
  const arrayContainer = document.getElementById("array-container");
  arrayContainer.innerHTML = "";

  const startingArrayDiv = document.createElement("div");
  startingArrayDiv.id = "starting-array";
  arrayContainer.appendChild(startingArrayDiv);

  currentArray = generateArray();
  fillArrContainer(startingArrayDiv, currentArray);
});

document.getElementById("sort-btn").addEventListener("click", function () {
  if (currentArray.length === 0) return;
  const arrayContainer = document.getElementById("array-container");

  const startingArrayDiv = document.getElementById("starting-array");
  fillArrContainer(startingArrayDiv, currentArray);
  highlightCurrentEls(startingArrayDiv, 0);
  
  let arr = currentArray.slice();
  const n = arr.length;
  let comparisonCount = 0; // total comparisons performed

  for (let i = 0; i < n - 1; i++) {
    for (let j = 0; j < n - i - 1; j++) {
      comparisonCount++;
      if (comparisonCount === 1) {
        if (!isOrdered(arr[j], arr[j + 1])) {
          swapElements(arr, j);
        }
      } else {
        const stepContainer = generateContainer();
        fillArrContainer(stepContainer, arr);
        highlightCurrentEls(stepContainer, j);
        arrayContainer.appendChild(stepContainer);

        if (!isOrdered(arr[j], arr[j + 1])) {
          swapElements(arr, j);
        }
      }
    }
  }

  const finalContainer = generateContainer();
  fillArrContainer(finalContainer, arr);
  arrayContainer.appendChild(finalContainer);
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

Your app is not going all the way to the end of the row

Please observe the example app operation and review the instructions for the algorithm.

I have tried all the possible methods
none of them are working

when step 18 is clearing
step 19 is having the issue
otherwise step18,19 are getting issues

Your app is not going all the way to the end of the row. Do you understand what I mean by this?

Log your loop variables. You should be looping over a maximum of each array item, then looping over each pair of numbers in the array.

Still it is not working

yes I understood it .
the code whatever I have written is working properly
in another editors

No it’s not. Your app does not go to the end of the row every time.


can you please help me to find out the logic

Did you write this code?

You don’t understand the logic?

i understood the code
i wrote the code
but step 18 is not getting cleared
i have written simple logic for bubble sort also

Please describe the algorithm that you use in English, how does it work?

1.comparion: here two adjacent elements will be compared
example : index j and index j+1 will get compared
2. swapping will be done
if the element at lower index i.e., j is greater than the element at the higher index j+1 .then they will get swapped.
then larger number will move right side

This process is done until the end

Ok, they swap, and then what?

after swapping it will pass to the next row
it will checks the elements in the next row
if no element found higher then no swapping is done

After swapping two numbers it immediately goes down to the next row?

How does the algo determine when to move on to the next row?

no
after swapping 2numbers it won’t go to next row

in the same row it checks the all the numbers
that means each row full cycle should complete

That’s not what it does. You can clearly see here after it compares 41 and 16 it swaps them and then moves to the next row. It does not then compare 41 and 43

The red boxes should go to the very end of the row every time, just as it did on the first pass.

In contrast you can see the example app the red boxes go to the end of the row every single pass.

Initial array: [79,69,24,42,31];

step1: compare 79 and 69swap → [69, 79, 24,42,31]

step2: compare 79 and 24 → swap → [69,24,79,42,31]

step3: compare 79 and 42→ swap →[69,24,42,79,31]

step4:compare 79 and 31 → swap →[69,24,42,31,79]

step5: compare 69 and 24 → swap → [24,69,42,31,79]

step6: compare 69 and 42 → swap →[24,42,69,31,79]

step7: compare 69 and 31 → swap → [24,42,31,69,79]

step8: compare 69 and 79 → no swap → [24,42,31,69,79]
step9:compare 24 and 42 → no swap → [24,42,31,69,79]

step 10: compare 42 and 31 → swap → [24,31,42,69,79]

step11: compare 42 and 69 → no swap→[24,31,42,69,79]

ste12: compare 69 and 79 → no swap → '[24,31 , 42,69,79]

step 12 is the final output

i made a raw iteration and the actual output is same
for (let i = 0; i < n - 1; i++) {
for (let j = 0; j < n - i - 1; j++) {

still step 18 is not getting cleared

still getting confused