scrimba
Note at 2:00
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 2:00
AboutCommentsNotes
Note at 2:00
Expand for more info
index.js
run
preview
console
// HINT: ONLY EDIT THE SPIN() AND STOP() FUNCTIONS

// globals
let pushed = false; // Has the stop button been pushed - false is default
let targetInt; // The target number to stop the wheel on
const spinningElem = document.getElementById('spinning'); // The spinning number
const button = document.getElementById("buttonPressed");
const result = document.getElementById('result');
const targetElem = document.getElementById('targetNum');

// event listener
button.addEventListener("click", buttonPressed);

// When the stop button is pushed
function buttonPressed() {
if (pushed) {
pushed = false;
button.innerHTML = 'STOP';
result.innerHTML = '';
setTargetInt();
spin();
} else {
pushed = true;
button.innerHTML = 'Play again!';
}
}

// 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 () => {
let currentInt = 0;
spinningElem.innerHTML = currentInt;

while (currentInt < 101) {
if (pushed) {
stop(currentInt);
break;
} else {
await sleep(75);

currentInt++;
if (currentInt === 101) {
currentInt = 0;
}

spinningElem.innerHTML = currentInt;
}
}
}

//EDIT THIS FUNCTION
function stop(currentInt) {
if (targetInt === currentInt) {
result.innerHTML = 'WHOOHOO! We have a winner!'
} else {
const delta = Math.abs(targetInt - currentInt);
result.innerHTML = `Yikes! You were off by ${delta}. Try again!`
}
}


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