Build a Sorting Visualizer - Build a Sorting Visualizer

Tell us what’s happening:

Hello FCC staff,
Testcase 18 fails for me. Is it a bug at FCC side or am i missing something? What exactly is expected out of this test case?
Please help, thanks!!

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 */
const generateButton = document.getElementById("generate-btn");
const sortButton = document.getElementById("sort-btn");

let arr = [];

const generateElement = () => {
  return Math.floor(Math.random() * 100 + 1);
};

const generateArray = () => {
  const array = [];
  for (let i = 0; i < 5; i++) {
    array[i] = generateElement();
  }
  return array;
};

const generateContainer = () => {
  return document.createElement("div");
};

const fillArrContainer = (html, arr) => {
  html.innerHTML = "";
  arr.forEach((element) => {
    const span = document.createElement("span");
    span.textContent = element;
    html.appendChild(span);
  });
};

const isOrdered = (x, y) => {
  return x <= y;
};

const swapElements = (arr, swapIndex) => {
  if (!isOrdered(arr[swapIndex], arr[swapIndex + 1])) {
    [arr[swapIndex], arr[swapIndex + 1]] = [arr[swapIndex + 1], arr[swapIndex]];
    return true;
  }
  return false;
};

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

generateButton.addEventListener("click", () => {
  arr = generateArray();
  const startingArray = document.getElementById("starting-array");
  const divsWithoutStartingArray = document.querySelectorAll(
    "#array-container > div:not(#starting-array)",
  );
  divsWithoutStartingArray.forEach((child) => {
    child.remove();
  });
  fillArrContainer(startingArray, arr);
});

sortButton.addEventListener("click", () => {
  highlightCurrentEls(document.getElementById("starting-array"), 0);
  for (let i = arr.length - 1; i > 0; i--) {
    let hasSwapped = false;
    for (let j = 0; j < i; j++) {
      if (swapElements(arr, j)) hasSwapped = true;
      const div = generateContainer();
      document.getElementById("array-container").appendChild(div);
      fillArrContainer(div, arr);
      highlightCurrentEls(div, j + 1);
    }
    if (!hasSwapped) {
      break;
    }
  }
  const lastDiv = document.querySelector("#array-container div:last-of-type");
  console.log(lastDiv);
  document.getElementById("array-container").appendChild(lastDiv);
  fillArrContainer(lastDiv, arr);
  lastDiv.style.border = "4px solid darkgreen";
});

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36

Challenge Information:

Build a Sorting Visualizer - Build a Sorting Visualizer

test your app (press the buttons in the preview), the last round is not complete

1 Like