scrimba
Note at 1:41
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

Note at 1:41
AboutCommentsNotes
Note at 1:41
Expand for more info
index.js
run
preview
console
//HINT: ONLY EDIT THE SPIN() AND STOP() FUNCTIONS

//globals
var pushed = false //Has the stop button been pushed - false is default
var targetInt; //The target number to stop the wheel on
var spinningElem = document.getElementById('spinning'); //The spinning number

//event listener
document.getElementById("buttonPressed").addEventListener("click", buttonPressed);

//When the stop button is pushed
function buttonPressed(){
pushed = true;
}

//set the target Int
function setTargetInt(){
var targetElem = document.getElementById('targetNum');
targetInt=Math.floor(Math.random() * 101)
targetElem.innerHTML = targetInt;
}

//sleep const
const sleep = (milliseconds) => {
return new Promise(resolve => setTimeout(resolve, milliseconds))
}



//EDIT THIS FUNCTION
const spin = async () => {
//WRITE YOUR CODE HERE
if (pushed) {
stop(parseInt(spinningElem.innerHTML)); //Trigger this function when the STOP button has been pushed
} else {
if (spinningElem.innerHTML !== "") {
spinningElem.innerHTML = parseInt(spinningElem.innerHTML) + 1;
} else {
spinningElem.innerHTML = "0";
}
await sleep(75) //Paste this wherever you need to sleep the incrimentor
spin()
}
}

//EDIT THIS FUNCTION
function stop(i){
//WRITE YOUR CODE HERE
const result = document.getElementById('result'); //display your result message here

if (i === targetInt) {
result.innerHTML = "Amazing, you win!! You hit the target!!";
} else {
const off = Math.abs(i - targetInt);
result.innerHTML = "Sorry, you lose. You are off by " + off;
}
}


//main
setTargetInt();
spin()
Console
/index.html
LIVE