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 id="hint-button" onclick="toggleHint()">ヒントを見る</div>
            <div id="hint-content"></div>
        </div>

        <div class="result-section" id="answer-section">
            <h2>回答結果</h2>
            <p id="answer-result"></p>
            <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()">もう一度遊ぶ</a></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;
}

/* 全問題数表示エリア */
.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;
}

#hint-content {
  display: none;
  padding: 10px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  margin-top: 10px;
}

#hint-button {
  background-color: #e0e0e0;
  padding: 10px 20px;
  border-radius: 10px;
  border: 2px solid black;
  cursor: pointer;
  text-align: center;
  margin-top: 20px;
}

#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;
}

              
            
!

JS

              
                const quizData = [
  {
    question: "ドラえもんの体の色は何色ですか?",
    choices: [{ text: "青" }, { text: "赤" }, { text: "緑" }, { text: "黄色" }],
    correct: 0,
    hint: "彼の体は海のような色です。",
  },
  {
    question: "ドラえもんの好きな食べ物は何ですか?",
    choices: [
      { text: "ピザ" },
      { text: "どら焼き" },
      { text: "カレー" },
      { text: "ケーキ" },
    ],
    correct: 1,
    hint: "名前に似た食べ物です。",
  },
  {
    question: "ドラえもんはどこから来ましたか?",
    choices: [
      { text: "未来" },
      { text: "過去" },
      { text: "月" },
      { text: "海底" },
    ],
    correct: 0,
    hint: "22世紀から来ています。",
  },
];
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);
  });

  // ヒントの初期化(閉じる)
  document.getElementById("hint-content").style.display = "none";
}

// 答えを確認
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 choicesContainer = document.getElementById("choices-container");
  choicesContainer.innerHTML = ""; // クリアして選択肢を再描画

  // 正解・不正解のメッセージ表示
  if (selected === questionData.correct) {
    resultText.innerHTML = "<span class='correct'>正解!</span>";
    score++;
  } else {
    resultText.innerHTML = "<span class='wrong'>不正解です。</span>";
  }

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

// ヒントを開閉できるアコーディオン
function toggleHint() {
  const hintContent = document.getElementById("hint-content");
  hintContent.textContent = currentQuiz[currentQuestion].hint; // ヒントの内容を表示
  if (hintContent.style.display === "none") {
    hintContent.style.display = "block";
  } else {
    hintContent.style.display = "none";
  }
}

// 次の問題へ
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