scrimba
Solution for day 24 of #javascriptmas
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

Solution for day 24 of #javascriptmas
AboutCommentsNotes
Solution for day 24 of #javascriptmas
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 () => {
for (let i = 0; i <= 101; i++) {
if (i == 101) {
i = 0;
}
let guess = 0;
guess = i;
await sleep(150);
spinningElem.innerHTML = guess;
if (pushed == true) {
stop(guess);
break;
}
}
}

//EDIT THIS FUNCTION
function stop(i){
var result = document.getElementById('result');
if (i == targetInt) {
result.innerText = `Spot on! 🎯`;
} else if (i >= targetInt - 5 && i <= targetInt + 5) {
console.log(i, targetInt + 5, targetInt - 5);
result.innerText = "So close!!! 🤦‍♀️";
} else {
result.innerText = "Not even close 😐"
}
}


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