Full Source Code

HTML + CSS + JavaScript — three-tab project. The most complete DOM manipulation teaching project ever built. Covers 9 core JS DOM skills in one playable game: createElement, appendChild, addEventListener, Math.random(), classList.add/remove, style.backgroundColor, querySelectorAll forEach, textContent, innerHTML reset. Study the JS tab in order — each function teaches a distinct concept. The most valuable 80 lines of JavaScript a beginner can read.

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  <title>RGB Color Guessing Game</title>
  <style>
    body { margin: 0; font-family: 'Arial', sans-serif; background-color: #232323; color: white; text-align: center; }
    h1 { background-color: #2c8da8; margin: 0; padding: 20px 0; font-size: 3em; text-transform: uppercase; line-height: 1.2; }
    #colorDisplay { display: block; font-size: 0.4em; color: white; font-weight: bold; }
    #stripe { background: white; height: 50px; text-align: center; color: black; line-height: 50px; }
    button { border: none; background: none; height: 100%; font-weight: bold; color: #2c8da8; letter-spacing: 1px; font-size: inherit; transition: all 0.3s; outline: none; padding: 0 10px; }
    button:hover { color: white; background: #2c8da8; }
    #container { max-width: 600px; margin: 40px auto; display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }
    .square { width: 100%; padding-bottom: 100%; background: purple; border-radius: 20px; transition: background 0.6s; cursor: pointer; }
    #message { display: inline-block; width: 20%; font-weight: bold; color: #2c8da8; }
    .selected { background: #2c8da8; color: white; }
    @media (max-width: 600px) {
      #container { grid-template-columns: repeat(2, 1fr); gap: 15px; margin: 20px auto; }
      h1 { font-size: 2.2em; }
      #colorDisplay { font-size: 0.3em; }
    }
  </style>
</head>
<body>
  <h1>The RGB Color Guessing Game<br><span id="colorDisplay">RGB</span></h1>
  <div id="stripe">
    <button id="reset">New Colors</button>
    <span id="message"></span>
    <button id="easyBtn">Easy</button>
    <button id="hardBtn" class="selected">Hard</button>
  </div>
  <div id="container"></div>

  <script>
    let numSquares = 6;
    let colors = [];
    let pickedColor;

    const colorDisplay  = document.getElementById("colorDisplay");
    const messageDisplay = document.getElementById("message");
    const h1            = document.querySelector("h1");
    const resetButton   = document.getElementById("reset");
    const easyBtn       = document.getElementById("easyBtn");
    const hardBtn       = document.getElementById("hardBtn");
    const container     = document.getElementById("container");

    init();

    function init() {
      setupModeButtons();
      setupSquares();
      reset();
    }

    function setupModeButtons() {
      easyBtn.addEventListener("click", function() {
        easyBtn.classList.add("selected");
        hardBtn.classList.remove("selected");
        numSquares = 3;
        reset();
      });
      hardBtn.addEventListener("click", function() {
        hardBtn.classList.add("selected");
        easyBtn.classList.remove("selected");
        numSquares = 6;
        reset();
      });
    }

    function setupSquares() {
      container.innerHTML = "";
      for (let i = 0; i < numSquares; i++) {
        const square = document.createElement("div");
        square.classList.add("square");
        square.addEventListener("click", function() {
          const clickedColor = this.style.backgroundColor;
          if (clickedColor === pickedColor) {
            messageDisplay.textContent = "Correct!";
            resetButton.textContent = "Play Again?";
            changeColors(clickedColor);
            h1.style.backgroundColor = clickedColor;
          } else {
            this.style.backgroundColor = "#232323";
            messageDisplay.textContent = "Try Again";
          }
        });
        container.appendChild(square);
      }
    }

    function reset() {
      colors = generateRandomColors(numSquares);
      pickedColor = pickColor();
      colorDisplay.textContent = pickedColor.toUpperCase();
      resetButton.textContent = "New Colors";
      messageDisplay.textContent = "";
      h1.style.backgroundColor = "#2c8da8";
      const squares = document.querySelectorAll(".square");
      for (let i = 0; i < squares.length; i++) {
        if (colors[i]) {
          squares[i].style.backgroundColor = colors[i];
          squares[i].style.display = "block";
        } else {
          squares[i].style.display = "none";
        }
      }
    }

    resetButton.addEventListener("click", reset);

    function changeColors(color) {
      document.querySelectorAll(".square").forEach(sq => {
        sq.style.backgroundColor = color;
      });
    }

    function pickColor() {
      const random = Math.floor(Math.random() * colors.length);
      return colors[random];
    }

    function generateRandomColors(num) {
      const arr = [];
      for (let i = 0; i < num; i++) {
        arr.push(randomColor());
      }
      return arr;
    }

    function randomColor() {
      const r = Math.floor(Math.random() * 256);
      const g = Math.floor(Math.random() * 256);
      const b = Math.floor(Math.random() * 256);
      return `rgb(${r}, ${g}, ${b})`;
    }
  </script>
</body>
</html>