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


const maxLoop = 10;
const waitTime = 250;

//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() * (maxLoop + 1))
targetElem.innerHTML = targetInt;
}

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


var curr_spin=-1;

//EDIT THIS FUNCTION
const spin = async () => {
var spinningElem = document.getElementById('spinning');
for(let spin=0; spin < maxLoop; spin++) {
spinningElem.innerHTML = spin;
console.log('spin:', spin, targetInt, curr_spin)

if (!pushed) {
//
} else if (spin == targetInt) {
console.log('Button pushed and won')
var result = document.getElementById('result');
result.innerText = "WON!";
spin = maxLoop+1;
} else {
console.log('Buitton pushed but missed')
spin = maxLoop+1;
}

await sleep(waitTime)
}
}

//main
setTargetInt();
spin();
Console
"spin:"
,
0
,
7
,
-1
,
"spin:"
,
1
,
7
,
-1
,
"spin:"
,
2
,
7
,
-1
,
"spin:"
,
3
,
7
,
-1
,
"spin:"
,
4
,
7
,
-1
,
"spin:"
,
5
,
7
,
-1
,
"spin:"
,
6
,
7
,
-1
,
"spin:"
,
7
,
7
,
-1
,
"spin:"
,
8
,
7
,
-1
,
"Buitton pushed but missed"
,
/index.html
LIVE