CpuHigherLower/gst.js

98 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-04-12 09:00:36 +02:00
var cpuList;
2023-04-12 21:42:33 +02:00
var currentCpu;
var score;
var highScore;
2023-04-12 22:19:37 +02:00
export async function main() {
2023-04-12 21:42:33 +02:00
// init cpu list
await (fetch('./data.json')
2023-04-12 09:00:36 +02:00
.then((response) => response.json())
2023-04-12 21:42:33 +02:00
.then((json) => cpuList = json));
nextRound();
}
function nextRound() {
currentCpu = getRandomCpu();
2023-04-12 09:00:36 +02:00
2023-04-12 21:42:33 +02:00
document.getElementById("cpuName").innerText = currentCpu.name;
2023-04-12 22:19:37 +02:00
document.getElementById("mainCol").style.backgroundColor = "";
2023-04-12 09:00:36 +02:00
}
function getRandomCpu() {
2023-04-12 21:42:33 +02:00
let randomIndex;
do {
randomIndex = getRandomInt(0, cpuList.length)
} while (typeof(cpuList[randomIndex]["type"]) == null)
2023-04-12 09:00:36 +02:00
return {
name: cpuList[randomIndex]["name"].split('@')[0],
2023-04-12 21:42:33 +02:00
type: cpuList[randomIndex]["type"]
2023-04-12 09:00:36 +02:00
}
}
2023-04-13 12:40:01 +02:00
function delay(time) {
return new Promise(resolve => setTimeout(resolve, time));
}
2023-04-12 22:19:37 +02:00
export async function btnClick(typ) {
// 0 -> Desktop
// 1 -> Laptop
// 2 -> Mobile/Embedded
// remove bootstrap class
let btnDesktop = document.getElementById("btnDesktop");
let btnLaptop = document.getElementById("btnLaptop");
let btnMobile = document.getElementById("btnMobile");
let btns = [btnDesktop, btnLaptop, btnLaptop];
btns.forEach( (el) => {
el.className = el.className.replace("bg-info", "");
})
switch (typ) {
case 0:
btnDesktop.style.backgroundColor = "#FF4444";
break;
case 1:
btnLaptop.style.backgroundColor = "#FF4444";
break;
case 2:
btnMobile.style.backgroundColor = "#FF4444";
break;
}
// make correct button green and wrong button red!!!! bg-success | bg-danger
switch (currentCpu.type) {
case "Desktop":
btnDesktop.style.backgroundColor = "lightgreen";
break;
case "Laptop":
btnLaptop.style.backgroundColor = "lightgreen";
break;
case "Mobile/Embedded":
btnMobile.style.backgroundColor = "lightgreen";
break;
}
await delay(2000);
btns.forEach( (el) => {
el.className = el.className + " bg-info";
el.style.backgroundColor = "";
})
nextRound();
}
2023-04-12 09:00:36 +02:00
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min); // The maximum is exclusive and the minimum is inclusive
}
main();