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

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

              
            
!

JS

              
                const quizData = [
  {
    question: "日本の首都はどこですか?",
    choices: [
      { text: "大阪" },
      { text: "京都" },
      { text: "東京" },
      { text: "札幌" },
    ],
    correct: 2,
  },
  {
    question: "富士山の標高は何メートルですか?",
    choices: [
      { text: "3,776メートル" },
      { text: "4,000メートル" },
      { text: "2,500メートル" },
      { text: "3,500メートル" },
    ],
    correct: 0,
  },
  {
    question: "日本で一番面積の大きい都道府県はどこですか?",
    choices: [
      { text: "北海道" },
      { text: "沖縄県" },
      { text: "愛知県" },
      { text: "兵庫県" },
    ],
    correct: 0,
  },
  {
    question: "日本で最も長い川はどれですか?",
    choices: [
      { text: "利根川" },
      { text: "信濃川" },
      { text: "淀川" },
      { text: "木曽川" },
    ],
    correct: 1,
  },
  {
    question: "桜の花が咲く時期は一般的にいつですか?",
    choices: [{ text: "夏" }, { text: "秋" }, { text: "冬" }, { text: "春" }],
    correct: 3,
  },
];
const totalQuestions = quizData.length;
document.getElementById("total-questions").textContent = totalQuestions;

let currentQuiz = [];
let currentQuestion = 0;
let score = 0;

// 問題をランダムに並び替える関数
function shuffleArray(array) {
  for (let i = array.length - 1; i > 0; i--) {
    const j = Math.floor(Math.random() * (i + 1));
    [array[i], array[j]] = [array[j], array[i]];
  }
  return array;
}

// 初期化
function initQuiz() {
  currentQuestion = 0; // 問題番号の初期化
  score = 0; // スコアの初期化
  currentQuiz = shuffleArray([...quizData]); // クイズデータをランダム化
  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 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 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