scrimba
Note at 0:37
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

AboutCommentsNotes
Note at 0:37
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
let i = 0;
while(i < 100 && !pushed) { // counter stops at 100
await sleep(250)
++i;
spinningElem.innerHTML = i;
}
stop(i);

}



//EDIT THIS FUNCTION
function stop(i){
//WRITE YOUR CODE HERE
var result = document.getElementById('result');
if(i !== targetInt) {
result.innerHTML = `Oh no! you lose off by ${Math.abs(targetInt - i)}`; //math abs converts - num to positive
} else {
result.innerHTML = `DANG DANG DANG YOU GOT IT CHAMP`
}

}


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