Tell us what’s happening:
18. After you click #sort-btn, #array-container should contain as many div elements as the steps required by the Bubble Sort algorithm to sort the starting array, including the div representing the starting array and a div representing the sorted array.
My code is able to pass everything except 18 and I don’t know what is the issue here. I’m also not sure what exactly 18 is asking for. I think I have the correct number of div element here under array-container. I also give the final div array the same style as the one in the example project. Can’t figure out what is missing in my code.
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 */
//******************************************/
//***
//***
//*** Functions
//***
//***
//******************************************/
function generateElement() {
let random = Math.floor(Math.random() * 100) + 1;
return random;
}
function generateArray() {
const arr = [];
while (arr.length < 5) {
arr.push(generateElement());
}
return arr;
}
function generateSpanContainer() {
const span = document.createElement('span');
span.innerHTML = ``;
return span;
}
function generateContainer() {
const div = document.createElement('div');
div.innerHTML = ``;
return div;
}
function fillArrContainer(el, arr) {
for (let i = 0; i < 5; i++) {
let span = generateSpanContainer();
span.innerHTML = `${arr[i]}`;
el.appendChild(span);
}
}
function isOrdered(int1, int2) {
return int1 <= int2 ? true : false;
}
function isArrayOrdered(arr) {
for (let i=0; i < arr.length; i++) {
if (arr[i] > arr[i+1]) {
return false;
}
}
return true;
}
function swapElements(arr, index) {
if (!isOrdered(arr[index], arr[index + 1])) {
let int1 = arr[index];
let int2 = arr[index + 1];
arr[index] = int2;
arr[index + 1] = int1;
}
}
function highlightCurrentEls(el, index) {
const childEl1 = el.children[index];
const childEl2 = el.children[index + 1];
const children = [childEl1, childEl2];
children.forEach((child) => {
child.style.borderColor = "red";
child.style.borderWidth = "2px";
child.style.borderStyle = "dashed";
})
}
//******************************************/
//***
//***
//*** DOM and Variable
//***
//***
//******************************************/
let array = [];
const generateBtn = document.getElementById("generate-btn");
const sortBtn = document.getElementById("sort-btn");
const arrayContainer = document.getElementById("array-container");
const startingArrayEl = document.getElementById("starting-array");
sortBtn.setAttribute('hidden', '');
//******************************************/
//***
//***
//*** Event Listeners
//***
//***
//******************************************/
generateBtn.addEventListener("click", () => {
while (arrayContainer.children.length > 1) {
arrayContainer.removeChild(arrayContainer.lastElementChild);
}
startingArrayEl.innerHTML = ``;
array = generateArray();
fillArrContainer(startingArrayEl, array);
sortBtn.removeAttribute('hidden', '');
})
sortBtn.addEventListener("click", () => {
highlightCurrentEls(startingArrayEl, 0);
swapElements(array, 0)
for (let i = 1; i < 4; i++) {
console.log(i);
let div = generateContainer();
fillArrContainer(div, array);
highlightCurrentEls(div, i);
swapElements(array, i);
arrayContainer.appendChild(div);
}
while (!isArrayOrdered(array)) {
for (let i = 0; i < 4; i++) {
console.log(i);
let div = generateContainer();
fillArrContainer(div, array);
highlightCurrentEls(div, i);
swapElements(array, i);
arrayContainer.appendChild(div);
}
}
if (isArrayOrdered(array)) {
let div = generateContainer();
for (let i = 0; i < 4; i ++) {
swapElements(array, i);
}
fillArrContainer(div, array);
div.style.border = "4px solid green";
arrayContainer.appendChild(div);
}
sortBtn.setAttribute('hidden', '');
}
)
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36
Challenge Information:
Build a Sorting Visualizer - Build a Sorting Visualizer


