Javascriptの練習のために、以下のようなクイズを作りました。
もう少し改良しようと、以下の挙動を導入したいと思っております。
「問題の文章の箇所は、1文字ずつ出てくるようにして、早押し形式にしてみる」
例)世・界・で・1・番・大・き・な〜 といった感じです。
setintervalなどを使って、100ms毎に問題を出していくような処理をするやり方などがある、と調べた結果わかったのですが、
もう少しヒント、特にとっかかりをどういう風に書いたらいいか等、のヒントを頂けると助かります。よろしくお願いいたします。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="utf-8"> <title>Quiz</title> <link rel="stylesheet" href="css/styles.css"> </head> <body> <div class="container"> <p id="timer"></p> <p id="scoreShow"></p> <p id="question"></p> <ul id="choices"> <!-- <li>A0</li> <li>A1</li> <li>A2</li> --> </ul> <div id="btn" class="disabled">Next</div> <section id="result"> <p></p> <a href="">REPLAY?</a> </select> </div> <script src="js/main.js"></script> </body> </html>
body { background: #efdec1; font-size: 14px; font-family: Verdana, sans-serif; } .container { width: 400px; margin: 8px auto; background: #fff; border-radius: 4px; padding: 16px; position: relative; } #question { margin-bottom: 16px; font-weight: bold; } #choices { list-style: none; padding: 0; margin-bottom: 16px; } #choices > li { border: 1px solid #ccc; border-radius: 4px; padding: 10px; margin-bottom: 10px; cursor: pointer; } #choices > li:hover{ background: #f8f8f8; } #btn { background: #3498db; padding: 10px; border-radius: 4px; cursor: pointer; color: #fff; text-align: center; box-shadow: 0 4px 0 #2880b9; } #btn.disabled { background: #ccc; box-shadow: 0 4px 0 #bbb; opacity: 0.7; } #choices > li.correct { background: #d4edda; border-color: #c3e6cb; color: #155724; font-weight: bold; } #choices > li.correct::after { content: "....Correct!"; } #choices > li.wrong { background: #f8d8da; border-color: #f5c6cb; color: #721c24; font-weight: bold; } #choices > li.wrong::after { content: "....Wrong!"; } #result { position: absolute; width: 300px; background: #fff; padding: 30px; box-shadow: 0 0 2px rgba(0, 0, 0, 0.5); top: -500px; left: 0; right: 0; margin: 0 auto; border-radius: 3px; text-align: center; transition: 0.4s ease-out; } #result > p { font-size: 14px; } #result > a { background: #3498db; padding: 10px; border-radius: 4px; cursor: pointer; color: #fff; text-align: center; box-shadow: 0 4px 0 #2880b9; display: block; text-decoration: none; } #result.show { top: 50px; } #btn.startgreen{ background: lightgreen; box-shadow: 0 4px 0 green; } #scoreShow { float: right; color: purple; }
'use strict'; { const question = document.getElementById('question'); const btn = document.getElementById('btn'); const choices = document.getElementById('choices'); const result = document.getElementById('result'); const timer = document.getElementById('timer'); const scoreLabel = document.querySelector('#result > p'); let scoreShow = document.getElementById('scoreShow'); let isAnswered = false; let countdown = false; let seconds = 10; let timerId; let isPlaying; const interval = 100; const quizSet = [ {q: '世界で一番大きな湖は?', c:['カスピ海', '黒海', '琵琶湖']}, ]; let currentNum = 0; let score = 0; function init(){ isPlaying = false; btn.classList.add('startgreen'); btn.classList.remove('disabled'); btn.textContent = 'Quiz Start'; } function checkAnswer(li) { if (li.textContent === quizSet[currentNum].c[0]){ li.classList.add('correct'); score++; } else { li.classList.add('wrong'); score--; } btn.classList.remove('disabled'); scoreShow.textContent = score; } function shuffle(arr){ let i = arr.length - 1; for(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 setQuiz(){ isAnswered = false; question.textContent = quizSet[currentNum].q; while(choices.firstChild){ choices.removeChild(choices.firstChild); } const shuffledChoices = shuffle([...quizSet[currentNum].c]); shuffledChoices.forEach(choice => { const li = document.createElement('li'); li.textContent = choice; li.addEventListener('click', () => { if(!isPlaying){ return; } if(isAnswered){ return; } checkAnswer(li); isAnswered = true; }); choices.appendChild(li); }); if(currentNum === quizSet.length -1) { btn.textContent = 'Check Result!'; } } function cDownStart(){ countdown = true; timer.innerHTML = seconds; timerId = setTimeout(cDown, 1000); } function cDown(){ seconds--; timer.innerHTML = seconds; if(seconds <= 0){ clearTimeout(); countdown = false; alert(`お疲れ様です。あなたのスコアは${score}点です。`); } else { setTimeout(cDown, 1000); } } init(); btn.addEventListener('click', () => { if(!isPlaying){ cDownStart(); setQuiz(); btn.textContent = 'Next'; btn.classList.remove('startgreen'); btn.classList.add('disabled'); isPlaying = true; return; } if(btn.classList.contains('disabled')) { return; } btn.classList.add('disabled'); if(currentNum === quizSet.length -1){ scoreLabel.textContent = `お疲れ様でした。あなたのスコアは${score}点です。`; result.classList.add('show'); } else { currentNum++; setQuiz(); } }); }

回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/04/09 14:27