Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

im utterly defeated now. please help with step 18 of this. each step should have a div for it and something.

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

function generateElement(){
  return Math.floor(Math.random() * 100) +1
}
function generateArray(){
  let genArr = []
  for(let i=0; i<5; i++){
    genArr.push(generateElement())
  }
  return genArr;
}

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

function fillArrContainer(el, arr){
  el.innerHTML = ""
  arr.forEach(num=>{
    const span = document.createElement("span");
    span.textContent = num
    el.appendChild(span)
  })
}
function isOrdered(num1, num2){
  return num1<=num2
}
function swapElements(arr, index){
  if(!isOrdered(arr[index], arr[index+1])){
  let previousNum = arr[index];
  arr[index] = arr[index+1];
  arr[index+1] = previousNum;
  return true} else return false
}
function highlightCurrentEls(el, index){
  const spans = el.querySelectorAll("span");
  spans.forEach(span=>{
    span.style.border = "none"
  })
  if (spans[index] && spans[index + 1]) {el.children[index].style.border = "2px dashed red"
  el.children[index+1].style.border = "2px dashed red"}
}
let currentArr = [];
generateBtn.addEventListener("click", ()=>{
  currentArr = generateArray();
  fillArrContainer(startingArr, currentArr);
  const stepDivs = arrContainer.querySelectorAll(".step-div")
  stepDivs.forEach(div=> div.remove())
  
})
sortBtn.addEventListener("click", ()=>{
  let arr = [...currentArr];
  let swapped = true;
  highlightCurrentEls(startingArr, 0)
  while (swapped) {
    swapped = false;
    for(let j=0; j<arr.length-1; j++){
     
     const container = generateContainer()
      container.className = "step-div";
      
     fillArrContainer(container, arr)
      highlightCurrentEls(container, j)
      arrContainer.appendChild(container);
     const wasSwapped = swapElements(arr, j);
      if (wasSwapped) {
        swapped = true;
      }
      
     
     
      
      
      
      
    }
  
  }
  //arrContainer.children[1].remove()
})

Your browser information:

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

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

Hey! The issue is about timing - look closely at your sortBtn event listener and check whether you’re creating/filling the containers BEFORE or AFTER you actually swap the elements.

Take a look at the example app. Generate an array and sort it there. Then take a screenshot.

Now use the same array in your code (temporarily hard coded) and compare how your code works to how the example app worked.

finally finished, was only missing the final sorted array