scrimba
Note at 0:02
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 0:02
AboutCommentsNotes
Note at 0:02
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 result = document.getElementById('result')
var targetElem = document.getElementById('targetNum')
var newGame = document.getElementById('buttonNew')
var stopButton = document.getElementById('buttonPressed')

//event listener
stopButton.addEventListener("click", buttonPressed);
newGame.addEventListener("click", startGame)

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

//set the target Int
function setTargetInt(){
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 (!pushed && i < 100) {
i++
await sleep(65)
spinningElem.innerHTML = i
await sleep(65)
}
if (i > 0) stop(i)
}

//EDIT THIS FUNCTION
function stop(i){
//WRITE YOUR CODE HERE
const missed = Math.abs(targetInt - i)
const message = (pushed) ? (missed === 0) ? 'You are a winner!!' : `You missed by ${missed} point(s)!` : 'You lost! You never pressed "Stop"!'

result.innerHTML = message;
stopButton.style.display = 'none';
newGame.style.display = 'block';
}

function startGame () {
result.innerHTML = '';
stopButton.style.display = 'block';
newGame.style.display = 'none';
setTargetInt();
pushed = false
spin();
}

//main
targetElem.innerHTML = '???';
spinningElem.innerHTML = '???';
Console
/index.html
LIVE