Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

Hie campers my solution works fine but i am failing test 15, i don’t know where i am getting it wrong . I need assistancee

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 Items ///////////////////////////////////////////////////////////////////

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

///////////////////////////////////////////// functions ////////////////////////////////////////////////////////////////

const generateElement = () => {
  return Math.ceil(Math.random() * 100);
}

const generateArray = () => {

  const randomNumArray = [];
  for(let i=0; i<5; i++){
    randomNumArray.push(generateElement())
  }

  return randomNumArray;
}

const generateContainer = () => {
  const newDiv = document.createElement('div');
  return newDiv;
}

const fillArrContainer = (container, intArr) =>{
  container.innerHTML = '';
  intArr.forEach(item=>{
    container.innerHTML += `<span>${item}</span>`;
  })
}

const isOrdered = (intOne, intTwo)=>{
  return intOne <= intTwo? true : false;
}

function swapElements(myArr, index){
    const indexOneOg = myArr[index];
    const indexTwoOg = myArr[index+1];
    
    if(myArr[index] <= myArr[index+1]){
        return myArr;
    }
    else if(myArr[index] >= myArr[index+1]){
        myArr.splice(index, 2,indexTwoOg,indexOneOg);
        return myArr;
    } 
}


function highlightCurrentEls(htmlElement, index){
  const elementArr = htmlElement.lastElementChild.querySelectorAll('span')

  elementArr[index].style.border = '2px dashed red';
  if (index + 1 < elementArr.length) {
    elementArr[index + 1].style.border = '2px dashed red';
  }
}

const getArrFromEl = (element, openingTag, closingTag) =>{
  let str = element.innerHTML;

  str = str.replaceAll(openingTag,'');
  return str = str.replaceAll(closingTag, ' ').trim().split(' ').map(item=> Number(item));
}

const resetArrContainer=()=>{
  const temporaryDivs = document.querySelectorAll('#array-container .sorting-sequence');
  temporaryDivs.forEach(div => div.remove());
  const temporarySpans = document.querySelectorAll('#starting-array span');
  temporarySpans.forEach(span=> span.remove());
}

function sortArray() {

  const generatedArr = getArrFromEl(startingArr, '<span>', '</span>');
  let status = true;
  let newDiv = '';

  while (status) {
    status = false;

    for (let i = 0; i < generatedArr.length - 1; i++) {
      
   //   const arrItems = arrContainer.lastElementChild.querySelectorAll('span');
      highlightCurrentEls(arrContainer, i);

      if (generatedArr[i] > generatedArr[i + 1]) {
        [generatedArr[i], generatedArr[i + 1]] = [generatedArr[i + 1], generatedArr[i]];
        status = true;
      }
      
        newDiv = generateContainer();
        generatedArr.forEach((item, index) => {
          const newSpan = document.createElement('span');
          newSpan.textContent = item;
          newDiv.append(newSpan);
        });
        newDiv.className = 'sorting-sequence';
        arrContainer.append(newDiv);
      
      
    }
    
  }
  const finalArr = arrContainer.lastElementChild.querySelectorAll('span');
  finalArr.forEach(item => item.style.border = 'none');
  arrContainer.lastElementChild.style.border = '4px solid green';
}




////////////////////////////////////////// Event Listeners  //////////////////////////////////////////////////////////////

generateBtn.addEventListener('click', ()=>{
  resetArrContainer();
  fillArrContainer(startingArr, generateArray());
  sortBtn.style.display='inline-block'
})

sortBtn.addEventListener('click',()=>{ 
  sortArray();
  
  const finalArr = arrContainer.lastElementChild.querySelectorAll('span');
  finalArr.forEach( item => item.style.border = 'none');
  arrContainer.lastElementChild.style.border = '4px solid green';
  sortBtn.style.display = 'none'
})

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////











Your browser information:

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

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

this htmlElement passed to the function, it’s not .array-container, it’s one of the divs inside it. Or at least the function is tested like that, with a div that contains span elements

i suppose i am passing #array-container element to the highlightCurrentEls in the sortArray function and in the #array-container i only have the #starting-array and the .sorting-sequence div.

if i was selecting passing a div inside #array-container, wouldnt elementArr be undefined

yes, that’s exactly the issue

the tests are passing a div with spans in it to highlightCurrentEls, so the tests fail

1 Like

thanks i have managed to solve the issue.