Explorer
project
index.css
index.html
index.js
solution.js
Dependencies
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
//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()