scrimba
Note at 1:59
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 1:59
AboutCommentsNotes
Note at 1:59
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 tryAgain = false
var targetInt; //The target number to stop the wheel on
var spinningElem = document.getElementById('spinning'); //The spinning number
var buttonText = document.getElementById('buttonPressed');

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

//When the stop button is pushed
function buttonPressed(){
if (tryAgain === true) {
reset();
} else {
pushed = true;
}
}

function reset(){
buttonText.innerHTML = 'STOP';
pushed = false;
tryAgain = false;
setTargetInt();
spin();
}

//set the target Int
function setTargetInt(){
var targetElem = document.getElementById('targetNum');
targetInt=Math.floor(Math.random() * 96) + 5
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 === 100) {
i =0
}
if (pushed === true) {
stop(i);
break;
} else {
spinningElem.innerHTML = i;
await sleep(75) ;
}
}
}

//EDIT THIS FUNCTION
function stop(i){
var adjustedI = i-1
var result = document.getElementById('result');
let targetDifference = Math.abs(targetInt - adjustedI)
//console.log(`targetInt=${targetInt}, i=${adjustedI}, targetDifference=${targetDifference}`)
if(targetDifference === 0) {
result.innerHTML = `you nailed it! ${targetDifference}`
} else if (targetDifference < 10){
result.innerHTML = `whoa, so close!! only off by ${targetDifference}`
} else {
result.innerHTML = `I know you can beat ${targetDifference}... try again?`
}
buttonText.innerHTML = 'try again?';
tryAgain = true;
}


//main
setTargetInt();
spin()
Console
"targetInt=96, i=19, targetDifference=77"
,
"targetInt=17, i=8, targetDifference=9"
,
"targetInt=73, i=15, targetDifference=58"
,
/index.html
LIVE