I hope this finds you well. I noticed the instructions say that the HTML and CSS are already provided, and the focus should be on completing the Bubble Sort Visualizer with JavaScript. Could you please confirm if my focus should be on JavaScript only?
Also, I often get stuck on test 18 and 19 and would really appreciate your guidance.
Go to the example app. Generate an array there and sort it, then take a screenshot of the results. Then replace the call to generateArray() in your code with a hard-coded array (only for testing!) that matches the one from your screenshot and compare the results.
I made some updates to my code and successfully passed Test 19, but I’m still stuck on Test 18. I’ve spent a lot of time on this challenge and can’t figure out what’s going wrong. Any help or insight would be hugely appreciated
const genBtn = document.getElementById("generate-btn");
const startingArray = document.getElementById("starting-array");
const arrayContainer = document.getElementById("array-container");
const sortBtn = document.getElementById("sort-btn");
function generateElement() {
return Math.ceil(100 * Math.random());
}
function generateArray() {
return [
generateElement(),
generateElement(),
generateElement(),
generateElement(),
generateElement()
];
}
function generateContainer() {
return document.createElement("div");
}
function fillArrContainer(htmlEl, arr) {
htmlEl.innerHTML = `
<span>${arr[0]}</span>
<span>${arr[1]}</span>
<span>${arr[2]}</span>
<span>${arr[3]}</span>
<span>${arr[4]}</span>
`;
}
function isOrdered(int1, int2) {
return int1 <= int2;
}
function swapElements(intArr, index) {
if (!isOrdered(intArr[index], intArr[index + 1])) {
[intArr[index], intArr[index + 1]] = [intArr[index + 1], intArr[index]];
}
}
function highlightCurrentEls(htmlEl, index) {
htmlEl.children[index].style.border = "1px dashed red";
htmlEl.children[index + 1].style.border = "1px dashed red";
}
genBtn.addEventListener("click", () => {
arrayContainer.innerHTML = "";
arrayContainer.appendChild(startingArray);
startingArray.innerHTML = "";
fillArrContainer(startingArray, generateArray());
});
sortBtn.addEventListener("click", () => {
// Clear old sort steps
while (arrayContainer.children.length > 1) {
arrayContainer.removeChild(arrayContainer.lastChild);
}
// Get current array from DOM
let currentArr = Array.from(startingArray.children).map(child =>
Number(child.textContent)
);
// Highlight first comparison
highlightCurrentEls(startingArray, 0);
// Bubble sort with visual steps
for (let i = 0; i < currentArr.length - 1; i++) {
let swapped = false;
for (let j = 0; j < currentArr.length - 1 - i; j++) {
// Swap if needed
if (currentArr[j] > currentArr[j + 1]) {
swapElements(currentArr, j);
swapped = true;
}
// Render the current state
let newContainer = generateContainer();
fillArrContainer(newContainer, currentArr);
highlightCurrentEls(newContainer, j);
arrayContainer.appendChild(newContainer);
}
if (!swapped) break;
}
// Final sorted array container
let lastContainer = generateContainer();
lastContainer.style.border = "4px solid green";
fillArrContainer(lastContainer, currentArr);
arrayContainer.appendChild(lastContainer);
});
Did you correct your algorithm to work the same way as the example?
Does your algo work like this?
When the last element of the sequence is reached, it starts a new cycle from the beginning of the sequence, and repeats the process until the elements are sorted. The algorithm stops after one cycle completes with no swaps.
If you need further assistance can you share a screenshot of your sorted array?
If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Help button located on the challenge. This button only appears if you have tried to submit an answer at least three times.
The Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.