ドットインストール - 3分動画でマスターできるプログラミング学習サービス

  • レッスン一覧
  • 料金プラン
    • レッスン一覧
    • 料金プラン
  • ログイン
  • 運営からのお知らせ

    • 2025/10/09 NEW

      【週刊ドットインストール】 Figma入門の制作に苦戦中...

    すべて見る
  • トップ /
  • マイページ /
  • main.js

main.js

'use strict';

{
  const question = document.getElementById('question');
  const choices = document.getElementById('choices');
  const btn = document.getElementById('btn');

  const quizSet = [
    {q: 'What is A?', c: ['A0', 'A1', 'A2']},
    {q: 'What is B?', c: ['B0', 'B1', 'B2']},
    {q: 'What is C?', c: ['C0', 'C1', 'C2']},
  ];
  let currentNum = 0;

  function shuffle(arr) {
    for (let i = arr.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [arr[j], arr[i]] = [arr[i], arr[j]];
    }
    return arr;
  }

  function checkAnswer(li) {
    if (li.textContent === quizSet[currentNum].c[0]) {
      li.classList.add('correct');
    } else {
      li.classList.add('wrong');
    }
  }

  function setQuiz() {
    question.textContent = quizSet[currentNum].q;
    const shuffledChoices = shuffle([...quizSet[currentNum].c]);
    shuffledChoices.forEach(choice => {
      const li = document.createElement('li');
      li.textContent = choice;
      li.addEventListener('click', () => {
        checkAnswer(li);
      });
      choices.appendChild(li);
    });
  }

  setQuiz();
}
dotinstall

3分動画でマスターできるプログラミング学習サービスです。あなたもプログラミングを始めてみませんか?

サービスメニュー

  • レッスン一覧
  • 料金プラン
  • 法人向けライセンス
  • 256times
  • 運営からのお知らせ
  • プログラミング学習ガイド
  • プログラミング学習相談室

ご利用にあたって

  • ヘルプ
  • 利用規約
  • プライバシーポリシー
  • 利用者情報の外部送信について
  • 運営企業情報
  • 採用情報
  • 教材利用について
  • お問い合わせ

ソーシャルメディア

  • note
  • X (@dotinstall)
  • Facebookページ

更新情報

  • 新着レッスンRSS
  • 新着補足情報RSS

Copyright © 2011-2025 dotinstall.com. All Rights Reserved.