// Globals
let targetInt; // The target number to stop the wheel on
let pushed = false // Has the stop button been pushed - false is default
// DOM Elements
const spinningElem = document.getElementById('spinning'); // The spinning number
const buttonElem = document.getElementById('buttonPressed');
const targetElem = document.getElementById('targetNum');
const resultElem = document.getElementById('result'); // Display your result message here
// Functions
const sleep = (ms) => new Promise(resolve => setTimeout(resolve, ms));
const buttonPressed = () => pushed = true;
const setTargetInt = () => {
targetInt = Math.floor(Math.random() * 101);
targetElem.innerText = targetInt;
}
const spin = async () => {
let i = 0;
while (!pushed) {
i = i >= 100 ? 0 : i + 1;
spinningElem.innerText = i;
await sleep(75) // Paste this wherever you need to sleep the incrimentor
}
stop(i); // Trigger this function when the STOP button has been pushed
}
const stop = (i) => {
const diff = Math.abs(i - targetInt);
if (diff === 0) {
resultElem.innerText = `Yay! You did it!`;
} else {
resultElem.innerText = `Oh no, you lose! Off by ${diff}`;
}
}
// Event listeners
buttonElem.addEventListener('click', buttonPressed);
// Init
setTargetInt();
spin();