Sample code

Game code

There are three small game pieces: a score, a guess, and a timer. Each one runs in one HTML page. Copy the code and open it on the test page.

Score

Each click adds one point. This is the smallest game: one number is kept, and one event changes it.

Test
<p>Xal: <b id="score">0</b></p>
<button type="button" id="hit">Vur</button>
<script>
  let score = 0;
  const scoreNode = document.getElementById("score");
  document.getElementById("hit").addEventListener("click", () => {
    score += 1;
    scoreNode.textContent = String(score);
  });
</script>

Guess

The hidden number is 2. One of the three buttons writes the right answer. The others say to try again. This is where a condition starts.

Test
<p>1, 2 və ya 3 seç.</p>
<button type="button" data-n="1">1</button>
<button type="button" data-n="2">2</button>
<button type="button" data-n="3">3</button>
<p id="out"></p>
<script>
  const secret = 2;
  const out = document.getElementById("out");
  document.querySelectorAll("button").forEach((button) => {
    button.addEventListener("click", () => {
      const pick = Number(button.getAttribute("data-n"));
      out.textContent = pick === secret ? "Düzdür" : "Yenə cəhd et";
    });
  });
</script>

Timer

Start counts down from 10 and stops at zero. The same button does not start twice, because it is disabled while the count is running.

Test
<p>Vaxt: <b id="time">10</b></p>
<button type="button" id="start">Başla</button>
<script>
  const start = document.getElementById("start");
  const time = document.getElementById("time");
  start.addEventListener("click", () => {
    let left = 10;
    start.disabled = true;
    const timer = setInterval(() => {
      left -= 1;
      time.textContent = String(left);
      if (left === 0) clearInterval(timer);
    }, 1000);
  });
</script>

A large game

Five questions, a score, and a restart. A right answer brings 10 points. When the questions end, the buttons close. The whole page is one file. Save it as .html and open it.

Test
<!DOCTYPE html>
<html lang="az">
<head>
  <meta charset="utf-8">
  <title>Quiz</title>
  <style>
    body { font-family: sans-serif; background: #0f172a; color: #e2e8f0; margin: 0; }
    main { max-width: 560px; margin: 32px auto; padding: 0 16px; }
    button { margin: 6px 6px 0 0; padding: 10px 14px; border: 0; border-radius: 8px; background: #0284c7; color: #fff; }
  </style>
</head>
<body>
<main>
  <p>Xal: <b id="score">0</b></p>
  <h1 id="ask"></h1>
  <div id="choices"></div>
  <p id="out"></p>
  <button type="button" id="again">Yenidən</button>
</main>
<script>
  const quiz = [
    { q: "Python nədir?", options: ["Brauzer", "Proqramlaşdırma dili", "Şəkil formatı"], answer: 1 },
    { q: "HTML nə üçündür?", options: ["Səhifənin quruluşu", "Cədvəlin parolu", "Telefonun batareyası"], answer: 0 },
    { q: "SQL nəyə sual verir?", options: ["Ekranın rənginə", "Klaviaturaya", "Verilənlərə"], answer: 2 },
    { q: "Bash nədir?", options: ["Şəkil redaktoru", "Əmr dili", "Cədvəl proqramı"], answer: 1 },
    { q: "CSS nəyi deyir?", options: ["Faylın parolunu", "Telefonun nömrəsini", "Səhifənin görünüşünü"], answer: 2 },
  ];
  let index = 0;
  let score = 0;
  const ask = document.getElementById("ask");
  const choices = document.getElementById("choices");
  const out = document.getElementById("out");
  const scoreNode = document.getElementById("score");

  function show() {
    choices.innerHTML = "";
    out.textContent = "";
    if (index >= quiz.length) {
      ask.textContent = "Bitdi";
      out.textContent = "Xal: " + score + " / " + (quiz.length * 10);
      return;
    }
    const item = quiz[index];
    ask.textContent = item.q;
    item.options.forEach((option, optionIndex) => {
      const button = document.createElement("button");
      button.type = "button";
      button.textContent = option;
      button.addEventListener("click", () => pick(optionIndex));
      choices.append(button);
    });
  }

  function pick(optionIndex) {
    const item = quiz[index];
    if (!item) return;
    if (optionIndex === item.answer) {
      score += 10;
      out.textContent = "Düzdür";
    } else {
      out.textContent = "Səhv";
    }
    scoreNode.textContent = String(score);
    choices.querySelectorAll("button").forEach((button) => { button.disabled = true; });
    index += 1;
    setTimeout(show, 600);
  }

  document.getElementById("again").addEventListener("click", () => {
    index = 0;
    score = 0;
    scoreNode.textContent = "0";
    show();
  });

  show();
</script>
</body>
</html>

About this tool

The game code section on Nibras Code provides three small script parts adding interactivity to web pages: score tracking, number guessing, and a countdown timer. Each part operates within a single HTML page, showing immediate results in the browser. These concise examples offer useful material for anyone learning the fundamentals of web programming.

People need such small code snippets when building web pages or adding simple interactive elements. The underlying concept is that browser scripts respond to events like button clicks or time intervals. Page text updates dynamically, reflecting user actions directly on the screen without complex setups or external libraries.

To use this material, simply copy the code and open it in a test page. You can save the file in any text editor and run it in a browser. Nibras Arabic and other recommended sections on the site are available for further exploration. Every sample is presented independently to keep the learning process straightforward.

How to use it

  1. Select the desired game code snippet from the page.
  2. Copy the code text to your clipboard.
  3. Paste the code into your own test page file.
  4. Open the file in a browser to test the result.

Common uses

Frequently asked questions

Where does this game code run?

Each part runs inside a single HTML page. You can copy the code and test it directly in any browser environment.

What is the correct answer in the guessing game?

The secret number is 2. One of the three buttons writes the correct answer, while the others prompt the user to try again.

How long does the timer count down?

The timer counts down from 10 when the start button is pressed and stops when it reaches zero, disabling itself during the count.