quizEngine/js/game.js

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2021-07-04 21:37:21 +03:00
// функции игры
export function imagePreloader(images, callback) {
let counter = 0;
2021-07-06 05:27:07 +03:00
// norm
function onLoad() {
counter += 1;
if (counter == images.length) callback();
}
let assetsDiv = document.getElementById("assets");
for (let i of images) {
let img = document.createElement('img');
img.onload = img.onerror = onLoad;
img.src = `assets/images/${i}`;
}
2021-07-06 05:27:07 +03:00
}
export function checkAnswer(quest, answer) {
2021-07-06 20:36:01 +03:00
if (Array.isArray(quest.rightAnswer)) {
let lowerCaseArray = [];
quest.rightAnswer.forEach(item => lowerCaseArray.push(item.toLowerCase()))
if (lowerCaseArray.includes(answer.toLowerCase())) {
quest.status = true;
}
else {
quest.status = false;
}
}
else {
if (quest.rightAnswer.toLowerCase() == answer.toLowerCase()) {
quest.status = true;
}
else
quest.status = false;
2021-07-04 23:24:27 +03:00
}
2021-07-04 22:12:04 +03:00
}
2021-07-04 23:07:38 +03:00
2021-07-06 05:27:07 +03:00
export function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
2021-07-04 23:07:38 +03:00
const j = Math.floor(Math.random() * (i + 1));
2021-07-06 05:27:07 +03:00
[array[i], array[j]] = [array[j], array[i]];
2021-07-04 23:07:38 +03:00
}
}
2021-07-06 06:10:22 +03:00
export function restartGame(game, quests) {
shuffle(quests);
quests.forEach(element => element.status = null);
game.finish = false;
game.currentQuest = 0;
}