scrimba
Note at 1:42
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:42
by
AboutCommentsNotes
Note at 1:42
by
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
var results = document.getElementById("result")
var refresh= document.getElementById("tryAgain")
document.getElementById("refresh")


//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 () => {
for (let i=0; i <101; i++) {

if(pushed){//true
stop(i);
break

}

if(i == 100){
i = 0;
}else{
spinningElem.innerHTML = i;
await sleep(75)
}
}

}

//EDIT THIS FUNCTION
function stop(stoppedNumber){
let remainder= Math.abs(targetInt - (stoppedNumber -1))

let message = remainder === 0
?"You Win!"
:`Oh no, you lose! by ${remainder}`

let result = document.getElementById('result');
result.innerHTML = `${message}`

let tryAgain = document.createElement("BUTTON")
tryAgain.innerHTML = "Try again?";
results.appendChild(tryAgain)
tryAgain.className = "refresh"

tryAgain.addEventListener("click", ()=>{
history.go(0);
})


}


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