以下のように国旗クイズを作りましたが、単なる乱数のため、同じ国が2回以上出てしまったりします。
重複しないような仕組みにしたいのですが、どのようにすることができるでしょうか? きっかけも掴めないため困っています。
(function() { 'use strict'; var question = document.getElementById('question'); var btn = document.getElementById('btn'); var input = document.getElementById('input'); var check = document.getElementById('check'); var timerLabel = document.getElementById('timer'); var scoreLabel = document.getElementById('score'); var answer = ''; var score = 0; var timerId; var isPlaying = false; var timer; var flag; var flags = [ ['Nepal.png', 'ネパール'], ['India.png', 'インド'], ['Netherland.png', 'オランダ'], ['Chili.png', 'チリ'], ['Morocco.png', 'モロッコ'], ['Luxembourg.png', 'ルクセンブルク'], ['Singapore.png', 'シンガポール'], ['Pakistan.png', 'パキスタン'], ['Azerbaijan.png', 'アゼルバイジャン'], ['Jamaica.png', 'ジャマイカ'], ['Belgium.png', 'ベルギー'], ['Guana.png', 'ガーナ'], ['SierraLeone.png', 'シエラレオネ'], ['Korea.png','韓国'], ['Japan.png','日本'], ['China.png','中国'], ['Mongolia.png','モンゴル'], ['Philippines.png', 'フィリピン'], ['Italy.png', 'イタリア'], ['Egypt.png', 'エジプト'], ['Australia.png','オーストラリア'], ['NewZealand.png', 'ニュージーランド'], ]; function init() { isPlaying = false; timer = 100; score = 0; scoreLabel.innerHTML = ''; scoreLabel.className = ''; timerLabel.className = ''; timerLabel.innerHTML = timer; btn.className = ''; check.className = 'invisible'; } init(); function updateTimer() { timerId = setTimeout(function() { timer--; if(timer <=10) { timerLabel.classList.add('danger'); } timerLabel.innerHTML = timer; if(timer <= 0) { alert('Game Overです。お疲れ様でした。あなたのスコアは ' + score + '点です。'); clearTimeout(); init(); return; } updateTimer(); }, 1000); } function setQuestion() { flag = flags[Math.floor(Math.random() * flags.length)]; question.children[0].src = 'img/' + flag[0]; answer = flag[1]; } btn.addEventListener('click', function() { if (!isPlaying) { btn.classList.add('invisible'); check.classList.remove('invisible'); isPlaying = true; updateTimer(); setQuestion(); } }); check.addEventListener('click', function() { if(input.value !== '' && isPlaying) { score += (input.value === answer)? 1: -1; if(score < 0) { scoreLabel.classList.add('danger'); } else { scoreLabel.className = ''; } scoreLabel.innerHTML = score; input.value = ''; input.focus(); setQuestion(); } }); })();
回答6件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/02/15 03:22
2019/02/15 03:44