Build a Sorting Visualizer - Test 18 & 20 don't pass

Tell us what’s happening:

I have been trying to solve this challenge for a few days but test 18 and 20 just don’t pass. I have also read through several other posts and this issue seems to occur often. I have no idea what I am doing wrong

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 */
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(element, array) {
  array.forEach(integer => element.innerHTML += `<span>${Number(integer)}</span>`)
}

function isOrdered(n1, n2) {
  return n1 <= n2;
}

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

function highlightCurrentEls(element, index) {
  const nthChild = element.children[index]
  const nthPlusOneChild = element.children[index + 1];

  nthChild.style.border = "1px dashed red";
  nthPlusOneChild.style.border = "1px dashed red";
}

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

const startingArray = document.getElementById("starting-array");

const arrayContainer = document.getElementById("array-container");


generateBtn.addEventListener("click", () => {
  arrayContainer.replaceChildren(startingArray);
  startingArray.replaceChildren();
  fillArrContainer(startingArray, generateArray());
})

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


sortBtn.addEventListener("click", () => {
  if (arrayContainer.children.length > 1) {
    console.log("Array is already sorted");
    return;
  }
  const spanEls = document.querySelectorAll("#starting-array span");
  const array = [];
  spanEls.forEach(span => array.push(Number(span.textContent)))
  for (let i = 0; i < array.length - 1; i++) {
    for (let j = 0; j < array.length - i - 1; j++) {
      if (i === 0 && j === 0) {
        highlightCurrentEls(startingArray, 0)
        swapElements(array, j);
        continue;
      };
      const div = generateContainer();
      arrayContainer.appendChild(div);
      fillArrContainer(div, array);
      highlightCurrentEls(div, j);
      swapElements(array, j);
    }
  }
  console.log(arrayContainer.children.length);

})

Your browser information:

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

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

GitHub Link: freeCodeCamp/curriculum/challenges/english/blocks/lab-sorting-visualizer/6716249b5405164036fd0b0d.md at main · freeCodeCamp/freeCodeCamp · GitHub

Hi @authdev7,

Compare the example app to your code.

Check your loop variables. Does your outer loop iterate over a maximum of the array length? Does your inner loop handle each pair of numbers in the array?

Happy coding

Hey dhess! Thanks for the hints. I have fixed the condition for the outer loop. But I dont understand what you mean about the inner loop.

Here is my updated code for sort handler (rest of the code is same as provided above):

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

  if (arrayContainer.children.length > 1) {

    console.log("Array is already sorted");

    return;

  }

  const spanEls = document.querySelectorAll("#starting-array span");

  const array = [];

  spanEls.forEach(span => array.push(Number(span.textContent)))

  for (let i = 0; i <= array.length - 1; i++) {

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

      

      const div = generateContainer();

      arrayContainer.appendChild(div);

      fillArrContainer(div, array);

      highlightCurrentEls(div, j);

      swapElements(array, j);

    }

  }

  console.log(arrayContainer.children.length);

})

This shows one iteration over an array. How many divs are there? How many pairs of highlighted numbers are there? What is the highest index of the array you should pass to highlightCurrentEls?

Im sorry but I still don’t get it.

There are 4 divs and 4 pairs of highlighted numbers. Imo, the highest index of array being passed should be 3 since highlightCurrentEls will affect (highlight) the passed index and index + 1 element

It sounds like you do get it. What are you doing to debug your code? Try logging the value of j. Does the value of j stay the same for each iteration of the outer loop?