Hi, I can’t pass the tests 18 and 19 of this project ( Certified Full Stack Developer Curriculum) and I don’t know why; could someone help me to fix these two issues and complete the project ?
-
- After you click
#sort-btn,#array-containershould contain as manydivelements as the steps required by the Bubble Sort algorithm to sort the starting array, including thedivrepresenting the starting array and adivrepresenting the sorted array.
- After you click
-
- After you click
#sort-btn, eachdivwithin#array-containershould contain fivespan, each with a number as its text, and arranged to represent the steps required by Bubble Sort algorithm to sort the starting array.
- After you click
Here is my Code:
function generateElement() {
return Math.floor(Math.random() * 100) + 1;
}
function generateArray() {
return Array.from({ length: 5 }, generateElement);
}
function generateContainer() {
return document.createElement('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])) {
[arr[index], arr[index + 1]] = [arr[index + 1], arr[index]];
}
}
function highlightCurrentEls(container, index) {
const children = container.children;
if (index < children.length - 1) {
children[index].style.border = '3px dashed red';
children[index + 1].style.border = '3px dashed red';
}
}
function bubbleSortVisualizer(arr) {
const arrayContainer = document.getElementById('array-container');
Array.from(arrayContainer.children).forEach(child => {
if (child.id !== 'starting-array') child.remove();
});
const startingContainer = document.getElementById('starting-array');
fillArrContainer(startingContainer, arr);
highlightCurrentEls(startingContainer, 0);
let swapped;
do {
swapped = false;
for (let i = 0; i < arr.length - 1; i++) {
const stepContainer = generateContainer();
fillArrContainer(stepContainer, arr);
highlightCurrentEls(stepContainer, i);
arrayContainer.appendChild(stepContainer);
if (arr[i] > arr[i + 1]) {
swapElements(arr, i);
swapped = true;
}
}
} while (swapped);
const finalContainer = generateContainer();
fillArrContainer(finalContainer, arr);
arrayContainer.appendChild(finalContainer);
}
document.getElementById('generate-btn').addEventListener('click', () => {
const arr = generateArray();
const arrayContainer = document.getElementById('array-container');
Array.from(arrayContainer.children).forEach(child => {
if (child.id !== 'starting-array') child.remove();
});
const startingContainer = document.getElementById('starting-array');
fillArrContainer(startingContainer, arr);
highlightCurrentEls(startingContainer, 0);
});
document.getElementById('sort-btn').addEventListener('click', () => {
const startingContainer = document.getElementById('starting-array');
const arr = Array.from(startingContainer.children).map(span => parseInt(span.textContent));
bubbleSortVisualizer(arr);
});
Thanks !!

