Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's 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 it's URL and the proper URL extention.

+ 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

Save Automatically?

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 http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>クイズ</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma@0.9.2/css/bulma.min.css">
  <script src="https://unpkg.com/vue@3.0.0/dist/vue.global.js"></script>
</head>

<body>
  <div id="app" class="container">
    <div class="columns is-mobile is-centered">
      <div class="column is-half">
        <h1 class="title is-1 has-text-centered">クイズ</h1>
        <qa-template v-if="currentQuestion" :question-text="currentQuestion.text" @click-yes-btn="doAnswer($event)"
          @click-no-btn="doAnswer($event)"></qa-template>
        <result-template v-if="!currentQuestion" :total-score="totalScore" :questions="questions"
          :your-answers="yourAnswers" @click="doRestart"></result-template>
      </div>
    </div>
  </div>
  <script type="text/x-template" id="qa-template">
    <div class="box">
      <div class="content">
        <p class="is-size-4">Q: {{ questionText }}</p>
        <div class="columns">
          <div class="column">
            <button class="button is-fullwidth is-success" @click="onClickYesBtn">YES</button>
          </div>
          <div class="column">
            <button class="button is-fullwidth is-warning" @click="onClickNoBtn">NO</button>
          </div>
        </div>
      </div>
    </div>
  </script>

  <script type="text/template" id="result-template">
    <div class="box">
    <div class="content has-text-centered">
      <h2 class="title">結果発表</h2>
      <table class="table is-borderd">
        <tr>
          <th v-for="(q, index) in questions">
            {{ index + 1 }}
          </th>
        </tr>
        <tr>
          <td v-for="correct in corrects" :class="getCorrectClassName(correct.cd)">
            {{ correct.value }}
          </td>
        </tr>
      </table>
      <p>あなたの得点は...</p>
      <p v-if="isPerfect" class="has-text-weught-bold">すごい!全問正解です!</p>
      <p>
        <strong class="is-size-2 has-text-danger">{{ totalScore }}</strong>/{{ maxPoint }}点でした
      </p>
      <button class="button is-fullwidth is-info has-text-weight-bold" @click="onClick">最初からやり直す</button>
    </div>
  </div>
  </script>

  <script src="app.js"></script>
</body>

</html>
              
            
!

CSS

              
                
              
            
!

JS

              
                'use strict';

const ANSWER = { YES: 1, NO: 0 };
const CORRECT = { CD: 0, VALUE: 'o' };
const INCORRECT = { CD: 0, VARUE: 'x' };
const MAX_POINT = 100;

const app = Vue.createApp({
  data: function () {
    return {
      questions: [
        {
          text: 'v-on:click の省略記法は #click である',
          answer: ANSWER.NO,
        },
        {
          text: '東京都は沖縄より面性が大きい?',
          answer: ANSWER.NO,
        },
        {
          text: '1円玉は水に浮かべることができない?',
          answer: ANSWER.NO,
        },
        {
          text: '元号を用いているのは日本のみである',
          answer: ANSWER.YES,
        },
      ],
      currentIndex: 0,
      yourAnswers: [],
    };
  },
  computed: {
    currentQuestion: function () {
      return this.questions[this.currentIndex];
    },
    correctAnswers: function () {
      const self = this;
      return this.questions.filter(function (question, index) {
        return question.answer === self.yourAnswers[index];
      });
    },
    totalScore: function () {
      const score = MAX_POINT / this.questions.length;
      return Math.floor(score * this.correctAnswers.length);
    },
  },
  methods: {
    doAnswer: function (answer) {
      this.yourAnswers[this.currentIndex] = answer;
      this.nextQuestion();
    },
    nextQuestion: function () {
      if (this.currentIndex < this.questions.length) {
        this.currentIndex += 1;
      }
    },
    doRestart: function () {
      this.currentIndex = 0;
      this.initYourAnswersArray();
    },
    initYourAnswersArray: function () {
      this.yourAnswers = Array(this.questions.length);
    },
  },
  mounted: function () {
    this.initYourAnswersArray();
  },
});

// 問題表示と解答のコンポーネント
app.component('qa-template', {
  props: {
    questionText: String,
  },
  template: '#qa-template',
  methods: {
    onClickYesBtn: function () {
      this.$emit('click-yes-btn', ANSWER.YES);
    },
    onClickNoBtn: function () {
      this.$emit('click-no-btn', ANSWER.NO);
    },
  },
});

// 結果表示コンポーネント
app.component('result-template', {
  props: {
    totalScore: {
      type: Number,
      required: true,
    },
    questions: Array,
    yourAnswers: Array,
  },
  template: '#result-template',
  computed: {
    corrects: function () {
      const self = this;
      return this.questions.map(function (q, i) {
        if (q.answer === self.yourAnswers[i]) {
          return { cd: CORRECT.CD, value: CORRECT.VALUE };
        } else {
          return { cd: INCORRECT.CD, value: INCORRECT.VARUE };
        }
      });
    },
    maxPoint: function () {
      return MAX_POINT;
    },
    isPerfect: function () {
      return this.totalScore == this.maxPoint;
    },
  },
  methods: {
    onClick: function () {
      this.$emit('click');
    },
    getCorrectClassName: function (correctCd) {
      return correctCd === CORRECT.CD ? 'has-text-weight-bold' : '';
    },
  },
});

app.mount('#app');

              
            
!
999px

Console