view source
JavaScript
document.title = 'ドラゴンクエスト10クイズ 知の祝祭';
let originalQuizList = [];
let quizPool = [];
fetch('./quizDqxData.csv')
.then(response => {
if (!response.ok) throw new Error("CSV読み込み失敗");
return response.text();
})
.then(csv => {
originalQuizList = parseCsvToQuiz(csv);
quizPool = [...originalQuizList]; // 初回セット
loadQuiz();
})
.catch(error => {
console.error("エラー:", error);
});
function parseCsvToQuiz(csv) {
const lines = csv.trim().split("\n");
const result = [];
for (const line of lines) {
const cells = line.split("\t").map(c => c.trim());
// 空行・空の質問を除外
if (cells.length < 2 || !cells[0]) continue;
const [question, ...choices] = cells;
result.push({
question,
choices,
correct: choices[0] // 最初が正解という前提
});
}
return result;
}
function loadQuiz() {
//const randomIndex = Math.floor(Math.random() * quizList.length);
//const quiz = quizList[randomIndex];
// ふくびきリストが空になったらリセット
if (quizPool.length === 0) {
quizPool = [...originalQuizList];
}
const randomIndex = Math.floor(Math.random() * quizPool.length);
const quiz = quizPool.splice(randomIndex, 1)[0]; // 抽選&削除
const shuffledChoices = quiz.choices
.map(text => ({ text, sort: Math.random() }))
.sort((a, b) => a.sort - b.sort)
.map(({ text }) => ({ text }));
displayQuiz({
h1: document.title,
question: quiz.question,
choices: shuffledChoices,
correct: quiz.correct
});
}
/*
このページでは、株式会社スクウェア・エニックスを代表とする共同著作者が権利を所有する画像を利用しております。 当該画像の転載・配布は禁止いたします。 (C)2019 ARMOR PROJECT/BIRD STUDIO/SQUARE ENIX All Rights Reserved.(C)SUGIYAMA KOBO(P)SUGIYAMA KOBO
*/
CSS
HTML
ページのソースを表示 : Ctrl+U , DevTools : F12
view-source:https://hi0a.com/demo/-js/js-dqx-quiz/
ABOUT
hi0a.com 「ひまあそび」は無料で遊べるミニゲームや便利ツールを公開しています。
プログラミング言語の動作デモやWEBデザイン、ソースコード、フロントエンド等の開発者のための技術を公開しています。
必要な機能の関数をコピペ利用したり勉強に活用できます。
プログラムの動作サンプル結果は画面上部に出力表示されています。
環境:最新のブラウザ GoogleChrome / Windows / Android / iPhone 等の端末で動作確認しています。
画像素材や音素材は半分自作でフリー素材配布サイトも利用しています。LINK参照。
仕様変更、API廃止等、実験途中で動かないページもあります。