scrimba
Note at 0:44
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:44
AboutCommentsNotes
Note at 0:44
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
var i = 0;
while (pushed == false) {
console.log(i)
await sleep(75) //Paste this wherever you need to sleep the incrimentor
i++;

spinningElem.innerHTML = i;

// wrap around back to zero
if (i == 100) {
i = 0;
}
}
stop(i); //Trigger this function when the STOP button has been pushed
}

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

var lose = targetInt == i ? false : true
if (lose) {
const difference = Math.abs(targetInt - i);
result.innerText = `Oh no, you lose! Off by ${difference}`
} else {
result.innerText = "Congratulations, you won!"
}
}


//main
setTargetInt();
spin()
Console
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
,
12
,
13
,
14
,
15
,
16
,
17
,
18
,
19
,
20
,
21
,
22
,
23
,
24
,
25
,
26
,
27
,
28
,
29
,
30
,
31
,
32
,
33
,
34
,
35
,
36
,
37
,
38
,
39
,
40
,
41
,
42
,
43
,
44
,
45
,
46
,
47
,
48
,
49
,
50
,
51
,
52
,
53
,
54
,
55
,
56
,
57
,
58
,
59
,
60
,
61
,
62
,
63
,
64
,
65
,
66
,
67
,
68
,
69
,
70
,
71
,
72
,
/index.html
LIVE