This commit is contained in:
51
RCV_QuizSpin2/tdv_sdk.js
Normal file
51
RCV_QuizSpin2/tdv_sdk.js
Normal file
@@ -0,0 +1,51 @@
|
||||
let data = []; // thay vì animals
|
||||
|
||||
// Hàm load dữ liệu từ file data.json
|
||||
async function loadData() {
|
||||
try {
|
||||
const res = await fetch("data.json");
|
||||
data = await res.json();
|
||||
} catch (error) {
|
||||
console.error("Lỗi load data:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
// Hàm chọn ngẫu nhiên 4 con và lấy 1 con làm đáp án đúng
|
||||
function getRandomQuestion() {
|
||||
if (data.length < 3) {
|
||||
console.error("Không đủ dữ liệu để chọn!");
|
||||
return null;
|
||||
}
|
||||
|
||||
// Shuffle
|
||||
const shuffled = [...data].sort(() => 0.5 - Math.random());
|
||||
|
||||
// Lấy 4 con đầu tiên
|
||||
const options = shuffled.slice(0, 3).map(item => ({
|
||||
...item,
|
||||
image: getImage(item.image)
|
||||
}));
|
||||
|
||||
// Chọn 1 trong 4 làm đáp án đúng
|
||||
const correct = options[Math.floor(Math.random() * options.length)];
|
||||
|
||||
window.items = {
|
||||
options: options,
|
||||
correct: correct
|
||||
};
|
||||
return window.items;
|
||||
}
|
||||
|
||||
|
||||
function voiceItem(name){
|
||||
const newAudio = new Audio();
|
||||
newAudio.src = "https://audio.senaai.vn/audio/en_female_1_"+name.toLowerCase()+".mp3";
|
||||
newAudio.play();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function getImage(name) {
|
||||
return "https://senaai.tech/games/RCV-QuizSpin2/" + name;
|
||||
}
|
||||
Reference in New Issue
Block a user