前提・実現したいこと
ご教授お願いいたします。
クイズのWebアプリケーションを作成していて、Buttonを動的に作成しvalue属性に選択肢を記述しようとしています。
データはopen triviaというフリーのデータベースサーバからJson形式で取得しています。
value属性に選択肢をセットする際、取得してきたデータから選択肢部分を抽出してvalue属性に設定しようと思っています。
発生している問題・エラーメッセージ
value属性に選択肢をセットする際、取得してきたデータから選択肢部分を抽出してvalue属性に設定しようと試みましたが、
設定されたデータが実態参照をしてしまうため、正しく文字列が出力されません。
該当のソースコード
javascript
1const startBtn = document.getElementById('start'); 2const topContent = document.getElementById('topContent'); 3const centerContent = document.getElementById('centerContent'); 4const downContent = document.getElementById('downContent'); 5const header = document.getElementById('header'); 6const content = document.getElementById('content'); 7let questionData; 8let questionNumber = 1; 9const corrects = []; 10const incorrects = []; 11const correctAnswers = []; 12const questionAnswers = { 13 0: [], 14 1: [], 15 2: [], 16 3: [], 17 4: [], 18 5: [], 19 6: [], 20 7: [], 21 8: [], 22 9: [] 23} 24 25Array.prototype.shuffle = function () { 26 let i = this.length; 27 while (i) { 28 let j = Math.floor(Math.random() * i); 29 --i; 30 [this[i],this[j]] = [this[j],this[i]]; 31 } 32 return this; 33} 34 35startBtn.addEventListener('click', () => { 36 waitingPage(); 37 startBtn.remove(); 38 asynchronousFunc() 39 .then(fetchCorrectAnswers) 40 .then(fetchAnswers) 41 .then(changeQuestionPage); 42}); 43 44const asynchronousFunc = () => { 45 return new Promise((resolve, reject) => { 46 fetch('https://opentdb.com/api.php?amount=10') 47 .then(response => response.json()) 48 .then(data => { 49 questionData = data; 50 resolve(); 51 }) 52 }); 53} 54 55const fetchCorrectAnswers = () => { 56 for (let i = 0; i < 10; i++) { 57 const correctAnswer = questionData.results[i].correct_answer; 58 correctAnswers.push(correctAnswer); 59 } 60} 61 62const fetchAnswers = () => { 63 for (let i = 0; i < 10; i++) { 64 const correctAnswer = questionData.results[i].correct_answer; 65 const inCorrectAnswer = questionData.results[i].incorrect_answers; 66 inCorrectAnswer.forEach(item => { 67 questionAnswers[i].push(item); 68 }); 69 questionAnswers[i].push(correctAnswer); 70 questionAnswers[i].shuffle(); 71 } 72} 73 74const waitingPage = () => { 75 header.innerText = '取得中'; 76 content.innerText = '少々お待ちください'; 77} 78 79const changeQuestionPage = () => { 80 header.innerText = '問題1'; 81 createCategory(); 82 createLevel(); 83 createQuestion(); 84 createAnswersBtn(questionNumber - 1); 85 86} 87 88const createCategory = () => { 89 const category = questionData.results[0].category; 90 const genre = document.createElement('h3'); 91 const genreContent = document.createTextNode('[ジャンル] ' + category); 92 genre.appendChild(genreContent); 93 topContent.appendChild(genre); 94} 95 96const createLevel = () => { 97 const difficulty = questionData.results[0].difficulty; 98 const level = document.createElement('h3'); 99 const levelContent = document.createTextNode('[難易度] ' + difficulty); 100 level.appendChild(levelContent); 101 topContent.appendChild(level); 102} 103 104const createQuestion = () => { 105 const question = questionData.results[0].question; 106 content.innerHTML = question; 107} 108 109const createAnswersBtn = number => { 110 for (let i = 0; i < questionAnswers[number].length; i++) { 111 const answerBtn = document.createElement('input'); 112 answerBtn.type = 'button'; 113 answerBtn.name = 'answerBtn'; 114 answerBtn.value = questionAnswers[number][i]; **◀︎こちらになります。** 115 answerBtn.onclick = clickBtn; 116 downContent.appendChild(answerBtn); 117 } 118} 119 120const clickBtn = event => { 121 ++questionNumber; 122 header.innerText = '問題' + questionNumber; 123 topContent.children[1].innerText = '[ジャンル] ' + questionData.results[questionNumber - 1].category; 124 topContent.children[2].innerText = '[難易度] ' + questionData.results[questionNumber - 1].difficulty; 125 changeAnswersBtn(questionNumber - 1); 126} 127 128const changeAnswersBtn = number => { 129 while (downContent.firstChild) { 130 downContent.removeChild(downContent.firstChild); 131 } 132 createAnswersBtn(number); 133} 134
Json
1{response_code: 0, results: Array(10)} 2response_code: 0 3results: Array(10) 40: 5category: "Entertainment: Film" 6correct_answer: "The Thing" 7difficulty: "easy" 8incorrect_answers: (3) ["Carrie", "Misery", "The Green Mile"] 9question: "Which of the following movies was not based on a novel by Stephen King? " 10type: "multiple" 11__proto__: Object 121: {category: "Science & Nature", type: "multiple", difficulty: "medium", question: "Along with Oxygen, which element is primarily responsible for the sky appearing blue?", correct_answer: "Nitrogen", …} 132: {category: "Entertainment: Film", type: "boolean", difficulty: "medium", question: "There aren't any live-action clones in "…ar Wars: Episode III - Revenge of the Sith".", correct_answer: "True", …} 143: {category: "Entertainment: Video Games", type: "boolean", difficulty: "hard", question: "In "Portal 2", Cave Johnson started out Aperture Science as a shower curtain company.", correct_answer: "True", …} 154: {category: "Geography", type: "multiple", difficulty: "medium", question: "Which of these countries is the smallest by population?", correct_answer: "Norway", …} 165: {category: "Celebrities", type: "multiple", difficulty: "medium", question: "In what year did "Bob Ross" die?", correct_answer: "1995", …} 176: {category: "General Knowledge", type: "multiple", difficulty: "medium", question: "Earl Grey tea is black tea flavoured with what?", correct_answer: "Bergamot oil", …} 187: {category: "Celebrities", type: "multiple", difficulty: "medium", question: "Which American celebrity died in 1977 playing golf in La Moraleja, Madrid?", correct_answer: "Bing Crosby", …} 198: {category: "Entertainment: Television", type: "multiple", difficulty: "easy", question: "On the NBC show Community, whose catch-phrase was "Pop! Pop!"?", correct_answer: "Magnitude", …} 209: {category: "Science & Nature", type: "multiple", difficulty: "hard", question: "Autosomal-dominant Compelling Helio-Ophthalmic Out…drome is the need to do what when seeing the Sun?", correct_answer: "Sneeze", …} 21length: 10 22__proto__: Array(0) 23__proto__: Object
回答1件
あなたの回答
tips
プレビュー