JavaScript - 24

JavaScript - 24: Random Background Color Changer

Create buttons that change the background color of the page to a random color. Also add a button to reset to black.

✦ JavaScript Editor
▶ Live Output

👀 Show Solution
function getRandomColor() {
  const letters = "0123456789ABCDEF";
  let color = "#";
  for (let i = 0; i < 6; i++) {
    color += letters[Math.floor(Math.random() * 16)];
  }
  return color;
}

function changeColor() {
  const newColor = getRandomColor();
  document.body.style.backgroundColor = newColor;
  document.getElementById("colorCode").textContent = newColor;
}