Tell us what’s happening:
I’ve seen the screenshot from staff of what the finished project should look like, and I’m stuck — the sorting algorithm works, but I can’t get startingArray with its first two elements highlighted to be the first div in arrayContainer. I’ve tried generating the container and filling it at a couple of different places followed by arrayContainer.appendChild at several places but can’t crack it. Is there a hint that could help me?
Your code so far
<!-- file: index.html -->
/* file: styles.css */
/* file: script.js */
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 array = [];
for (let i = 0; i < 5; i++) {
array.push(generateElement());
}
console.log('Generated Array:', array);
return array;
}
function generateContainer() {
return document.createElement('div');
}
function fillArrContainer(element, array) {
while (element.firstChild) {
element.removeChild(element.firstChild);
}
array.forEach(item => {
const span = document.createElement('span');
span.textContent = item;
element.appendChild(span);
});
}
function isOrdered(n, m) {
return (n <= m);
}
function swapElements(array, index) {
if (index < 0 || index >= array.length - 1) {
throw new Error('Index out of bounds');
}
if (!isOrdered(array[index], array[index + 1])) {
[array[index], array[index + 1]] = [array[index + 1], array[index]];
}
}
function highlightCurrentEls(parent, index) {
const child1 = parent.children[index];
const nextChild = parent.children[index + 1];
if (child1) child1.style.border = "dashed red 4px";
if (nextChild) nextChild.style.border = "dashed red 4px";
}
function resetHighlights(parent) {
Array.from(parent.children).forEach(child => {
child.style.border = "";
});
}
function clearSnapshots() {
Array.from(arrayContainer.children).forEach(child => {
if (child.id !== 'starting-array') child.remove();
});
}
async function bubbleSort(array) {
const n = array.length;
// Highlight startingArray
resetHighlights(startingArray);
highlightCurrentEls(startingArray, 0);
for (let i = 0; i < n - 1; i++) {
let swapped = false;
for (let j = 0; j < n - 1; j++) {
// Clear previous highlights
resetHighlights(startingArray);
// SNAPSHOT BEFORE SWAP: Create and highlight.
const stepContainer = generateContainer();
fillArrContainer(stepContainer, [...array]);
highlightCurrentEls(stepContainer, j);
arrayContainer.appendChild(stepContainer);
// Delay to allow the user to see the highlights
await new Promise((resolve) => setTimeout(resolve, 100));
if (!isOrdered(array[j], array[j + 1])) {
[array[j], array[j + 1]] = [array[j + 1], array[j]];
swapped = true;
}
}
if (!swapped) {
break;
}
}
const finalContainer = generateContainer();
fillArrContainer(finalContainer, [...array]);
finalContainer.style.border = "5px solid green";
arrayContainer.appendChild(finalContainer);
resetHighlights(startingArray);
}
generateBtn.addEventListener("click", () => {
clearSnapshots();
const array = generateArray();
fillArrContainer(startingArray, array);
});
sortBtn.addEventListener("click", async () => { clearSnapshots();
clearSnapshots();
const array = Array.from(startingArray.children).map(child => parseInt(child.textContent));
await bubbleSort(array);
});
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Safari/605.1.15 Ddg/26.2
Challenge Information:
Build a Sorting Visualizer - Build a Sorting Visualizer
