CpuHigherLower/gst.js

41 lines
884 B
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 09:00:36 +02:00
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 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
}
}
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();