Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!DOCTYPE html>
<html lang="ja">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>雑学クイズ</title>
    <link rel="stylesheet" href="styles.css">
</head>

<body>

    <div class="container" id="container">
        <!-- 全問題数表示 -->
        <div class="quiz-info">
            全 <span id="total-questions"></span> 問
        </div>

        <h1>雑学クイズ</h1>

        <div class="quiz-container" id="quiz-container">
            <div class="question-number" id="question-number"></div>
            <div class="question" id="question"></div>
            <div class="grid-container" id="choices-container"></div>
        </div>

        <div class="result-section" id="answer-section">
            <h2>回答結果</h2>
            <p id="answer-result"></p>
            <h3>各選択肢の解説</h3>
            <div id="explanation"></div>
            <div id="next-question" onclick="nextQuestion()">次の問題へ</div>
        </div>

        <div id="final-result" class="final-section">
            <h2>最終結果</h2>
            <p id="score"></p>
            <div id="restart-quiz" onclick="restartQuiz()">もう一度遊ぶ</div>
        </div>
    </div>

    <!-- 先に問題ファイルを読み込む -->
    <script src="quizData.js"></script>
    <!-- 次にメインのスクリプトを読み込む -->
    <script src="script.js"></script>
</body>

</html>
              
            
!

CSS

              
                body {
  font-family: Arial, sans-serif;
  background-color: #f9f9f9;
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  margin: 0;
}

.container {
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
  text-align: center;
  padding: 50px;
}

/* 全問題数表示エリア */
.quiz-info {
  font-size: 1.2rem;
  color: white;
  margin-bottom: 20px;
  max-width: 100px;
  margin-left: auto;
  margin-right: auto;
  padding: 5px;
  border: 2px solid #333;
  border-radius: 5px;
  background-color: dimgrey;
}

.quiz-container,
.result-section,
.final-section {
  display: none;
  margin: 20px auto;
}

.question-number {
  font-weight: bold;
  margin-bottom: 10px;
}

.question {
  font-weight: bold;
  margin-bottom: 10px;
  padding: 15px;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
  background-color: #ffffff;
}

.grid-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-gap: 10px;
}

.choice {
  padding: 15px;
  background-color: #ffffff;
  text-align: center;
  cursor: pointer;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
  transition: background-color 0.3s;
}

.choice:hover {
  background-color: #d0d0d0;
}

.result-section {
  padding: 20px;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
  background-color: #f1f1f1;
}

#answer-result {
  font-size: 2rem;
  margin-bottom: 10px;
}

.explanation {
  margin-top: 10px;
}

.correct {
  color: green;
}

.wrong {
  color: red;
}

#next-question {
  margin-top: 20px;
  padding: 10px;
  background-color: #e0e0e0;
  text-align: center;
  cursor: pointer;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
  transition: background-color 0.3s;
  max-width: 300px;
  margin-left: auto;
  margin-right: auto;
}

#next-question:hover {
  background-color: #d0d0d0;
}

.final-section {
  padding: 20px;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 5px 5px 10px rgba(0, 0, 0, 0.5);
  background-color: #f9f9f9;
  margin: 20px auto;
  max-width: 800px;
}

#restart-quiz {
  margin-top: 20px;
  padding: 10px;
  background-color: #e0e0e0;
  text-align: center;
  cursor: pointer;
  border: 2px solid black;
  border-radius: 10px;
  box-shadow: 0px 0px 8px rgba(0, 0, 0, 0.5);
  transition: background-color 0.3s;
  max-width: 300px;
  margin-left: auto;
  margin-right: auto;
}

#restart-quiz:hover {
  background-color: #d0d0d0;
}

.knowledge-box {
  border: 2px solid black;
  padding: 10px;
  margin-top: 10px;
  background-color: #f9f9f9;
}

              
            
!

JS

              
                const quizData = [
  {
    question: "地球上で最も大きい動物は何ですか?",
    choices: [
      {
        text: "ゾウ",
        explanation:
          "不正解。ゾウは陸上では最大の動物ですが、全体で見ればもっと大きな動物がいます。",
      },
      {
        text: "シロナガスクジラ",
        explanation:
          "正解です! シロナガスクジラは地球上で最も大きな動物で、全長30メートル以上に達することがあります。",
      },
      {
        text: "サメ",
        explanation:
          "不正解。サメも大きな生物ですが、シロナガスクジラほど大きくはありません。",
      },
      {
        text: "キリン",
        explanation:
          "不正解。キリンは首が長いですが、全体の大きさでは他の動物に劣ります。",
      },
    ],
    correct: 1,
    knowledge:
      "シロナガスクジラは海に生息し、体重が150トンにもなることがあります。",
  },
  {
    question: "人間の体の中で最も長い骨は何ですか?",
    choices: [
      {
        text: "鎖骨",
        explanation: "不正解。鎖骨は首の近くにある短い骨です。",
      },
      {
        text: "大腿骨",
        explanation:
          "正解です! 大腿骨(太ももの骨)は人間の体の中で最も長い骨です。",
      },
      {
        text: "肋骨",
        explanation:
          "不正解。肋骨は胸郭を形成する骨ですが、大腿骨ほど長くありません。",
      },
      {
        text: "肩甲骨",
        explanation: "不正解。肩甲骨は背中の上部にある平たい骨です。",
      },
    ],
    correct: 1,
    knowledge:
      "大腿骨は体重を支えるために非常に強く、人間の成長とともに最も長くなります。",
  },
  {
    question: "世界で最も高い山はどこですか?",
    choices: [
      {
        text: "キリマンジャロ",
        explanation:
          "不正解。キリマンジャロはアフリカで最も高い山ですが、世界一ではありません。",
      },
      {
        text: "富士山",
        explanation:
          "不正解。富士山は日本で最も高い山ですが、世界一ではありません。",
      },
      {
        text: "エベレスト",
        explanation:
          "正解です! エベレストは標高8,848メートルで、世界で最も高い山です。",
      },
      {
        text: "マッターホルン",
        explanation:
          "不正解。マッターホルンはヨーロッパの有名な山ですが、エベレストには及びません。",
      },
    ],
    correct: 2,
    knowledge:
      "エベレストはヒマラヤ山脈にあり、標高は8,848メートルで世界一高い山です。",
  },
];
let currentQuiz = quizData; // 全問題をそのまま使用
let currentQuestion = 0;
let score = 0;
const totalQuestions = quizData.length;
document.getElementById("total-questions").textContent = totalQuestions;

// 初期化
function initQuiz() {
  currentQuestion = 0; // 問題番号の初期化
  score = 0; // スコアの初期化
  loadQuestion();
}

// 問題と選択肢を表示
function loadQuestion() {
  document.getElementById("quiz-container").style.display = "block";
  document.getElementById("answer-section").style.display = "none";
  document.getElementById("final-result").style.display = "none";

  // 問題番号を表示
  document.getElementById("question-number").textContent = `第 ${
    currentQuestion + 1
  } 問`;

  const questionData = currentQuiz[currentQuestion];

  document.getElementById("question").textContent = questionData.question;
  const choicesContainer = document.getElementById("choices-container");
  choicesContainer.innerHTML = "";

  // 選択肢に番号を付ける
  questionData.choices.forEach((choice, index) => {
    const choiceDiv = document.createElement("div");
    choiceDiv.classList.add("choice");
    choiceDiv.textContent = `${index + 1}. ${choice.text}`; // 番号付きの選択肢
    choiceDiv.onclick = () => checkAnswer(index, questionData);
    choicesContainer.appendChild(choiceDiv);
  });
}

// 答えを確認
function checkAnswer(selected, questionData) {
  document.getElementById("quiz-container").style.display = "none";
  document.getElementById("answer-section").style.display = "block";

  const resultText = document.getElementById("answer-result");
  const explanationDiv = document.getElementById("explanation");
  explanationDiv.innerHTML = "";
  explanationDiv.style.textAlign = "left"; // 左寄せ

  if (selected === questionData.correct) {
    resultText.innerHTML = "<span class='correct'>正解!</span>";
    score++;
  } else {
    resultText.innerHTML = "<span class='wrong'>不正解です。</span>";
  }

  // 各選択肢の解説を表示(番号付き)
  questionData.choices.forEach((choice, index) => {
    const explanationPara = document.createElement("p");
    if (index === questionData.correct) {
      explanationPara.innerHTML = `<span style="color: red; font-weight: bold;">${
        index + 1
      }. ${choice.text}</span> : ${choice.explanation}`;
    } else if (index === selected) {
      explanationPara.innerHTML = `<span style="color: blue;">${index + 1}. ${
        choice.text
      }</span> : ${choice.explanation}`;
    } else {
      explanationPara.innerHTML = `${index + 1}. ${choice.text} : ${
        choice.explanation
      }`;
    }
    explanationDiv.appendChild(explanationPara);
  });

  // 予備知識を枠で囲む
  const knowledgePara = document.createElement("div");
  knowledgePara.classList.add("knowledge-box");
  knowledgePara.innerHTML =
    "<strong>予備知識:</strong> " + questionData.knowledge;
  explanationDiv.appendChild(knowledgePara);

  // 最終問題かどうかのチェック
  if (currentQuestion === currentQuiz.length - 1) {
    document.getElementById("next-question").textContent = "結果を見る";
  } else {
    document.getElementById("next-question").textContent = "次の問題へ";
  }
}

// 次の問題へ
function nextQuestion() {
  currentQuestion++;
  if (currentQuestion < currentQuiz.length) {
    loadQuestion();
    document.getElementById("container").scrollIntoView({ behavior: "smooth" }); // containerにスクロール
  } else {
    showResult();
  }
}

// 結果を表示
function showResult() {
  document.getElementById("answer-section").style.display = "none";
  document.getElementById("final-result").style.display = "block";

  const percentage = (score / currentQuiz.length) * 100;
  document.getElementById("score").textContent = `正解数: ${score}/${
    currentQuiz.length
  } (${percentage.toFixed(2)}%)`;
}

// もう一度遊ぶ
function restartQuiz() {
  currentQuestion = 0;
  score = 0;
  initQuiz();
}

// クイズ開始
initQuiz();

              
            
!
999px

Console