Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

I can’t get past the 18th test. I don’t see the problem in the inspector and I have even compared the html results with the example. Please help.

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 */
//DOM references
const generateBtn = document.getElementById("generate-btn");
const startingArray = document.getElementById("starting-array");
const sortBtn = document.getElementById("sort-btn");
const arrayContainer = document.getElementById("array-container");

//functions
const generateElement = () => Math.floor(Math.random() * 100 + 1);

const generateArray = () => Array.from({length: 5}, generateElement);

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

const fillArrContainer = (e, arr) => arr.forEach((i) => {
  const span = document.createElement("span");
  span.textContent = i;
  span.setAttribute("id",`number-${i}`);
  e.appendChild(span);});

const isOrdered = (a, b) => a <= b;

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

const highlightCurrentEls = (e, i) => {
  e.children[i].style.border = "dashed";
  e.children[i].style.borderColor = "red";
  e.children[i].style.borderWidth = "2px";
  e.children[i+1].style.border = "dashed";
  e.children[i+1].style.borderColor = "red";
  e.children[i+1].style.borderWidth = "2px";
}

//Events
generateBtn.addEventListener("click", () => {
  if (startingArray.innerHTML){
    startingArray.innerHTML = "";

     Array.from(arrayContainer.children).forEach(child => {
    if (child !== startingArray) {
      arrayContainer.removeChild(child);
    }
    });
  }
  fillArrContainer(startingArray, generateArray());
  });

sortBtn.addEventListener("click", () => {

    Array.from(arrayContainer.children).forEach(child => {
    if (child !== startingArray) {
      arrayContainer.removeChild(child);
    }
  });

  let array = Array.from(startingArray.querySelectorAll("span"), span => Number(span.textContent));
  let bucles = array.length - 1;

  for(let i = 0; i < bucles; i++) {
    for (let j = 0; j < bucles -i; j++) {
      const lastDiv = arrayContainer.lastElementChild;
      highlightCurrentEls(lastDiv, j);
      swapElements(array, j);
      const newDiv = generateContainer();
      fillArrContainer(newDiv, array);
      
      arrayContainer.appendChild(newDiv);
    }

  }
  const sortedArray = arrayContainer.lastElementChild;
  sortedArray.style.borderColor = "green"
})



Your browser information:

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

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer
https://www.freecodecamp.org/learn/full-stack-developer/lab-sorting-visualizer/build-a-sorting-visualizer

Use the example app to complete a sort and take a screenshot of the completed sort.

Hardcode the same array into your app for testing and take a screenshot of the completed sort.

They should be exactly the same.