Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

I dont understand why it always stops after five iterations. Idk my code should make it continue until sorted

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */

function generateElement(){
  return Math.floor(Math.random()*(101-1)+1);
}

function generateArray(){
  const arr = Array(5).fill(-1);

  for(let i = 0; i < arr.length; i++){
    arr[i] = generateElement();
  }
  return arr;
}

// console.log(generateArray())

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

function fillArrContainer(elem, arr){
  for(let i = 0; i < arr.length; i++){
    elem.innerHTML += `<span>${arr[i]}</span>`
  }

  return elem;
}

// console.log(fillArrContainer(generateContainer(), generateArray()))

function isOrdered (int1, int2){
  if(int1 <= int2){
    return true;
  }else{
    return false;
  }
}

function swapElements(intArr, numIndex){
  if(isOrdered(intArr[numIndex], intArr[numIndex +1]) == false){
    const temp = intArr[numIndex +1];
    intArr[numIndex +1] = intArr[numIndex];
    intArr[numIndex] = temp;
    
  }
  return intArr;
}

function highlightCurrentEls(elem, intIndex){
  const childNodes = elem.childNodes;

  childNodes[intIndex].style.border = "dashed";
  childNodes[intIndex +1].style.border = "dashed";

  childNodes[intIndex].style.borderColor = "red";
  childNodes[intIndex+1].style.borderColor = "red";

  childNodes[intIndex].style.borderWidth = "5p";
  childNodes[intIndex+1].style.borderWidth ="5p";
}

const generateBtn = document.getElementById("generate-btn");
const startingArr = document.getElementById("starting-array");
const arrayContainer = document.getElementById("array-container");
const sortBtn = document.getElementById("sort-btn");

let currArr;

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

function checkIfSort(arr){
  const last = arr[arr.length-1];

  for(let i = 0; i < arr.length; i++){
     for(let j = i+1; j < arr.length; j++){
      if(arr[i] > arr[j]){
        return false;
      }
     }
    
  }

  return true
}

console.log(checkIfSort([14, 58, 30,66,82]))

function sorter(){
  let count = 0;
  let upmost = currArr.length;

  do{
    let newDiv = arrayContainer.appendChild(generateContainer());
    fillArrContainer(newDiv, currArr);
    highlightCurrentEls(newDiv, count);
    swapElements(currArr, count);

    if(count < upmost){
      count++;
      
    }else{
      count = 0;
      
    }

    if(checkIfSort(currArr)){
      let newDiv = arrayContainer.appendChild(generateContainer());
      fillArrContainer(newDiv, currArr);
      highlightCurrentEls(newDiv, count);
      swapElements(currArr, count);
    }
  }while(!checkIfSort(currArr))
      
 if(checkIfSort(currArr)){
    return;
  }
}


sortBtn.addEventListener("click", sorter)

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/153.0.0.0 Safari/537.36 OPR/137.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

Hi @hayden.wasted,

Put some logs in your code so you can see what going on. If you log the parameters passed to highlightCurrentEls, you’ll see that it eventually breaks because the function is attempting to add 1 to an index of 4.

Happy coding

Thank you for the help. it now works properly but the tests still wont pass.

function generateElement(){
  return Math.floor(Math.random()*(101-1)+1);
}

function generateArray(){
  const arr = Array(5).fill(-1);

  for(let i = 0; i < arr.length; i++){
    arr[i] = generateElement();
  }
  return arr;
}

// console.log(generateArray())

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

function fillArrContainer(elem, arr){
  for(let i = 0; i < arr.length; i++){
    elem.innerHTML += `<span>${arr[i]}</span>`
  }

  return elem;
}

// console.log(fillArrContainer(generateContainer(), generateArray()))

function isOrdered (int1, int2){
  if(int1 <= int2){
    return true;
  }else{
    return false;
  }
}

function swapElements(intArr, numIndex){
  if(isOrdered(intArr[numIndex], intArr[numIndex +1]) == false){
    const temp = intArr[numIndex +1];
    intArr[numIndex +1] = intArr[numIndex];
    intArr[numIndex] = temp;
    
  }
  return intArr;
}

function highlightCurrentEls(elem, intIndex){
  const childNodes = elem.childNodes;

  console.log(intIndex)
  // console.log(elem)

  if(intIndex == undefined){
    return;
  }

  if(intIndex == 4){
    childNodes[intIndex].style.border = "dashed";
    childNodes[intIndex].style.borderColor = "red";
    childNodes[intIndex].style.borderWidth = "5p";
    console.log("maxed")
    return;
  }

  childNodes[intIndex].style.border = "dashed";
  childNodes[intIndex +1].style.border = "dashed";

  childNodes[intIndex].style.borderColor = "red";
  childNodes[intIndex+1].style.borderColor = "red";

  childNodes[intIndex].style.borderWidth = "5p";
  childNodes[intIndex+1].style.borderWidth ="5p";
}

const generateBtn = document.getElementById("generate-btn");
const startingArr = document.getElementById("starting-array");
const arrayContainer = document.getElementById("array-container");
const sortBtn = document.getElementById("sort-btn");

let currArr;

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

function checkIfSort(arr){
  const last = arr[arr.length-1];

  for(let i = 0; i < arr.length; i++){
     for(let j = i+1; j < arr.length; j++){
      if(arr[i] > arr[j]){
        return false;
      }
     }
    
  }

  return true
}

console.log(checkIfSort([14, 58, 30,66,82]))
let count = 0;

function bubbleSort(){
  
  
  

  for(let i = 0; i < currArr.length-1; i++){
    if(count == 0 && i == 0){
       count++;
       continue;
    }else{

      let newDiv = arrayContainer.appendChild(generateContainer());
      fillArrContainer(newDiv, currArr);
      highlightCurrentEls(newDiv, i);
      swapElements(currArr, i);


      if(checkIfSort(currArr)){
        // let newDiv = arrayContainer.appendChild(generateContainer());
        // fillArrContainer(newDiv, currArr);
        // highlightCurrentEls(newDiv, i);
        // swapElements(currArr, i);

      return;
      }
    }
    count++;
  }
  return currArr;
}

function sorter(){
  highlightCurrentEls(startingArr, 0)
  swapElements(currArr, 0);

  while(!checkIfSort(currArr)){
    bubbleSort();
  }

  let newDiv = arrayContainer.appendChild(generateContainer());
  fillArrContainer(newDiv, currArr);
  // highlightCurrentEls(newDiv, currArr.length);
  swapElements(currArr, currArr.length);

  // sortBtn.classList.add("hide")
 
}


sortBtn.addEventListener("click", sorter)

Please compare the way your code behaves to the example app.

Your code is still not including the starting array as part of the bubble sort, and it stops on the first div that’s sorted instead of stopping after one full cycle completes with no swaps as indicated in the instructions.

thank you for the help.