scrimba
Note at 0:48
Go Pro!

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:48
AboutCommentsNotes
Note at 0:48
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
let currentNum = 0;
while (currentNum < 101) {
if (pushed === true) {
stop(currentNum); //Trigger this function when the STOP button has been pushed
break;
} else {
await sleep(75) //Paste this wherever you need to sleep the incrimentor
currentNum += 1;
}
spinningElem.innerHTML = currentNum;
}
stop(currentNum);
}

//EDIT THIS FUNCTION
function stop(stoppedAt){
//WRITE YOUR CODE HERE
var result = document.getElementById('result'); //display your result message here
const diff = Math.abs(stoppedAt - targetInt);
const winMessages = [
"You won! 🎉 You've got the reflexes of a cat! Meow 😸",
"🎉 You won, congratulations! Have you considered entering the lottery? 🤑",
"Holy cow, you won! 👼 🐄 🎉",
"Ding ding ding! We've got a winner here! 🔔 🎉",
"🎊 You got it! Way to go! 😌"
];
const loseMessages = [
"😞 Too bad, so sad, you lost.",
"😫 Better luck next time, friend.",
"😴 You missed it, maybe you were sleeping?"
]
if (stoppedAt === 101) {
result.innerHTML = "🤖 So, we just went ahead and stopped it for you. We didn't want your device to melt.";
return false;
}
if (diff === 0) {
result.innerHTML = winMessages[Math.floor(Math.random() * winMessages.length)];
} else {
result.innerHTML = `${loseMessages[Math.floor(Math.random() * loseMessages.length)]} You were off by ${diff}.`;
}
return true;
}


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