Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

My code can’t pass step 19 ( 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.), even though it should. There is a problem with the code in the bubble sort algorithm: it doesn’t go through the whole section which could be a cause(I’m not sure).

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: script.js */
const generateElement = () => {
  const randNum = Math.floor((Math.random() * 100) + 1);
  return randNum;
}

const generateArray = () => {
  const newArr = [];

  for (let i = 0; i < 5; i++) {
    newArr.push(generateElement());
  }
  
  return newArr;
}

const generateContainer = () => {
  return document.createElement("div");
}

const fillArrContainer = (htmlEl, arr) => {
  htmlEl.innerHTML = "";
  for (const arrEl of arr) {
    htmlEl.innerHTML += `<span>${arrEl}</span>`;
  }
}

const isOrdered = (int1, int2) => int1 <= int2;

const swapElements = (arr, i) => {
  if (!isOrdered(arr[i], arr[i + 1])) {
    let firstEl = arr[i];
    arr[i] = arr[i + 1];
    arr[i + 1] = firstEl;
  }
}

const highlightCurrentEls = (htmlEl, i) => {
  const borderHighlight = `2px dashed red`;
  htmlEl.children[i].style.border = borderHighlight;
  htmlEl.children[i + 1].style.border = borderHighlight;
}

const generateBtn = document.getElementById("generate-btn");
const sortBtn = document.getElementById("sort-btn");

generateBtn.addEventListener("click", () => {
  const startingArr = document.getElementById("starting-array");
  const arrContainer = document.getElementById("array-container");
  
  if (startingArr.textContent || arrContainer.textContent) {
    startingArr.innerHTML = "";
    arrContainer.innerHTML = "";

    arrContainer.appendChild(startingArr);
  }
  
  let array = generateArray();
  fillArrContainer(startingArr, array);
  
  sortBtn.disabled = false;
});

sortBtn.addEventListener("click", () => {
  const startingArr = document.getElementById("starting-array");
  const arrContainer = document.getElementById("array-container");

  let spanArray = Array.from(startingArr.children);
  let array = spanArray.map((span) => parseInt(span.textContent));
  
  if (startingArr.textContent || arrContainer.textContent) {
    startingArr.innerHTML = "";
    arrContainer.innerHTML = "";
  }
   
  fillArrContainer(startingArr, array);
  highlightCurrentEls(startingArr, 0); 
  arrContainer.appendChild(startingArr);

  let comparisonCount = 0;

  for (let i = 0; i < array.length - 1; i++) {
    for (let j = 0; j < array.length - 1; j++) {
      if (comparisonCount === array.length - 1) {
        break;
      }

      const swapDivContainer = generateContainer();

      fillArrContainer(swapDivContainer, array);
      highlightCurrentEls(swapDivContainer, j);
      arrContainer.appendChild(swapDivContainer);

      if (!isOrdered(array[j], array[j + 1])) {
        swapElements(array, j); 
        comparisonCount = 0;
      } else if (isOrdered(array[j], array[j + 1]) && j > 0) {
        comparisonCount++;
      }
    }
  }
  const finalDivContainer = generateContainer();
  fillArrContainer(finalDivContainer, array);
  arrContainer.appendChild(finalDivContainer);

  //console.log(arrContainer.innerHTML.split("><"));
  console.log(arrContainer.innerHTML.split("><").join("\n\n"));

  sortBtn.disabled = true;
});
/* 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;
}

#array-container>div:last-of-type {
    border: 4px solid green;
}

#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;
  }
}

Here’s a bubble sort algorithm from the wiki that I tried(it fails on step 18):


  let swapped = false;
  do {
    swapped = false;
    for (let j = 0; j < array.length - 1; j++) {
      const swapDivContainer = generateContainer();

      fillArrContainer(swapDivContainer, array);
      highlightCurrentEls(swapDivContainer, j);
      arrContainer.appendChild(swapDivContainer);

      if (!isOrdered(array[j], array[j + 1])) {
        swapElements(array, j); 
        swapped = true;
      }
    }
  } while (swapped)

Your browser information:

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

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

I would fix this first, as it will definitely cause a problem with the tests.

I discovered the issue with swapped and fixed it, however, why would steps 18 and 19 be marked as passed if I simply add j > 0 or else statement to if clause?