scrimba
Note at 1:23
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:23
AboutCommentsNotes
Note at 1:23
Expand for more info
index.js
run
preview
console
/*
DESCRIPTION:
In this challenge a casino has asked you to make an online dice that works just like
it would in real life. Using the pre-made dice face that represents ‘one’, make the
faces for ‘two’, ‘three’, ‘four’, ‘five’ and ‘six’. Now when the users clicks the
dice on the screen the dice is expected to show one of the faces randomly.

event listeners, Math.random()

*/

// Write your code here 👇


/*

DETAILED INSTRUCTIONS
1. pick out the neccesary elements from the HTML
2. Create other 5 dice faces in CSS
3. use eventlisteners on the appropriate div
4. Display dice faces randomly on click

STRETCH GOALS:
- Can you show the number you rolled as a integer along-side the dice face?
- Can you improve the overall design?
*/

const diceRoll = () => {
let roll = Math.floor(Math.random() * 6) + 1;
document.querySelector(".number").firstChild.nodeValue = roll
switch (roll) {
case 1:
document.querySelector(".dice").className = "dice one"
break;
case 2:
document.querySelector(".dice").className = "dice two"
break;
case 3:
document.querySelector(".dice").className = "dice three"
break;
case 4:
document.querySelector(".dice").className = "dice four"
break;
case 5:
document.querySelector(".dice").className = "dice five"
break;
case 6:
document.querySelector(".dice").className = "dice six"
break;
default:
return
}
}
document.querySelector(".dice").addEventListener("click", diceRoll);
Console
/index.html
LIVE