Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

For some reason my code keeps outputing only 11 steps in the bubble sort and i dont understand why. Someone 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 */
function generateElement(){
  return Math.floor(Math.random() * 100 + 1)
}

function generateArray(){
  let arr = []

  for (let i = 0; i <= 4; i++){
    arr.push(generateElement())
  }

  return arr
}

function generateContainer(){
  let newDiv = document.createElement("div")
  return newDiv
}

function fillArrContainer(el, arr){
 
  for (let i = 0; i <= 4; i++){
    let span = document.createElement("span")
    span.innerHTML = arr[i]
    el.appendChild(span)
  }

  return el
  
}

function isOrdered(n1, n2){
  if (n1 <= n2){
    return true
  }else{
    return false
  }
}

function swapElements(arr, indx){
  if (!isOrdered(arr[indx], arr[indx + 1])){
    let temp = arr[indx]
    arr[indx] = arr[indx + 1]
    arr[indx + 1] = temp
    return true
  }
  return false
}

function highlightCurrentEls(el, indx){

  let children = el.children

  children[indx].style.border = "2px dashed red"

  children[indx + 1].style.border = "2px dashed red"

}

let generateBtn = document.getElementById("generate-btn")
let startingArr = document.getElementById("starting-array")
let sortBtn = document.getElementById("sort-btn")
let arrCont = document.getElementById("array-container")

generateBtn.addEventListener("click", e => {
  
  startingArr.innerHTML = ""

  Array.from(arrCont.children).forEach(child => {
    if (child !== startingArr){
      child.remove()
    }
  })

  fillArrContainer(startingArr, generateArray())


})

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

   Array.from(arrCont.children).forEach(child => {
    if (child !== startingArr){
      child.remove()
    }
  })


  let list = startingArr.querySelectorAll("span")

  if (list.length === 0){
    return
  }

  let array = Array.from(list, span => parseInt(span.textContent))


  highlightCurrentEls(startingArr, 0)


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

         swapElements(array, j)
       
         let newDiv = generateContainer()
         fillArrContainer(newDiv, array)
         arrCont.appendChild(newDiv)
         highlightCurrentEls(newDiv, j)

    

    }
  }

})



Your browser information:

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

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

do you know what this is doing?

bigger snippet so you can find it easier

its making it not loop over already sorted stuff right?

If i dont have that line in my code i fail test 19, but if i have it im only failing test 20 and 18. And I dont understand how to pass step 18. I have been stuck on it for some hours.

Don’t just remove a whole line, troubleshoot.

Log your i and j values in the loop to confirm the values are what you want them to be.

I still dont get it. I think my values are right, atleast when i log them.

Obviously, you loop variables are not right since the inner loop should be handling every pair of numbers in the array for each iteration of the outer loop, but your code is not doing that:

i: 0 j: 0
i: 0 j: 1
i: 0 j: 2
i: 0 j: 3
-------------> from here on down it's borked
i: 1 j: 0
i: 1 j: 1
i: 1 j: 2
i: 2 j: 0
i: 2 j: 1
i: 3 j: 0

Show me.

What should they be? What are they actually?