Build a Sorting Visualizer Test #17

Tell us what’s happening:

I have all tests passed except #17 and have tried everything. Functions the way the test is requiring it to, and should pass, but isn’t. What should I be doing differently?

Your code so far

<!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>
        <div id="starting-array"></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>

* {
    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;
  }
}

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

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() { 
  const div = document.createElement("div") 
  return div; 
  }; 
  
function fillArrContainer(container, arr) { 
  container.innerHTML = ""; 
  arr.forEach(num => { 
    const span = document.createElement("span"); 
    span.textContent = num; 
    container.appendChild(span); 
  }) 
}; 
  
function isOrdered(a, b) { 
  return a <= b; 
}; 

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

function highlightCurrentEls(container, index) { 
  const children = container.children; 
  if (children[index]) { 
    children[index].style.border = "2px dashed red"; 
  } 
  if (children[index + 1]) { 
    children[index + 1].style.border = "2px dashed red"; 
  } 
}; 


let arr = []; 

generateBtn.addEventListener("click", () => {
  arr = generateArray();
  arrayContainer.innerHTML = "";
  startingArray.innerHTML = "";
  arr.forEach(num => {
    const span = document.createElement("span");
    span.textContent = num;
    startingArray.appendChild(span);
  });
});

sortBtn.addEventListener("click", () => { 
  if (arr.length === 0) return; 
  arrayContainer.innerHTML = ""; 
  const workingArr = [...arr]; 
  const steps = []; 
  steps.push([...workingArr]); 
  let swapped; 
  do { 
    swapped = false; 
    for (let i = 0; i < workingArr.length - 1; i++) { 
      const stepContainer = generateContainer(); 
      fillArrContainer(stepContainer, workingArr); 
      highlightCurrentEls(stepContainer, i); 
      arrayContainer.appendChild(stepContainer); 
      if (!isOrdered(workingArr[i], workingArr[i + 1])) { 
        swapElements(workingArr, i); 
        swapped = true; 
      } 
    } 
  } while (swapped); 
  
  const finalContainer = generateContainer(); 
  fillArrContainer(finalContainer, workingArr); 
  arrayContainer.appendChild(finalContainer); 
  highlightCurrentEls(startingArray, 0); 
})



Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:144.0) Gecko/20100101 Firefox/144.0

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

https://www.freecodecamp.org/learn/full-stack-developer/lab-sorting-visualizer/build-a-sorting-visualizer

What is test 17?

Which specific lines do you think meet the requirements of test 17?

Compare what your code generates to what the example project generates. Where is your starting array after the sort? What is missing from your final div?

How are you doing on this? Have you discovered the first area in your code that needs to be revised?

Why did you change the HTML code that was provided?

<div id="array-container">
    <div id="starting-array"></div>
</div>

HTML code was changed because the div being in the other div was failing multiple other tests. Making the div independent divs allowed other tests to pass. Without this the array was not even populating on the browser everytime you would click ‘generate array’.

Already did, I do not see anything in the results that shows that #17 should be failing. From the instructions given to what I am seeing on screen I am having a hard time figuring out what exactly it wants because from my perspective the code already does what the question is requiring.

I’m assuming there is an issue with the timing of either the generateArray() method or the fillArrContainer() after clicking ‘generate array’ with an array already sorted, but the feedback has not been useful at all.

I suggest you reset your HTML to what was provided and think about using an alternative approach since I don’t think you will ever be able to pass all the tests leaving that change in place. Besides, it borks the styling.

When that’s done, start by looking at your generate button listener. In it, you are blowing away the starting array when you write arrContainer.innerHTML = "";. So, think about how you can put that populated div back into arrayContainer. HINT: You already have a function that can do that.

Hmm…from what I’m seeing using the code given, I’m also not fulfilling test 16 and 21 either.

Seems generated array doesn’t show up when I press the generate array button, which it should.

Here’s one of the problems, first of all. This.

arrayContainer.innerHTML = "";

Check your HTML to find the element which the var arrayContainer is mapped to.

What do you think would happen by initializing the innerHTML to this?

Also, secondly, when you press the Generate Array button again, you need to remove all the divs in the array container that isn’t the #starting-array one.

How do you suppose you can do that?

(EDIT: I also had trouble with this project for a while, so don’t worry. :D)