Explorer
project
index.html
index.js
style.css
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
/*
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()
*/
const refresh = document.querySelectorAll(".dot");
const centerDot = document.getElementById("centerDot");
const dot1 = document.getElementById("dot1");
const dot2 = document.getElementById("dot2");
const dot3 = document.getElementById("dot3");
const dot4 = document.getElementById("dot4");
const dot5 = document.getElementById("dot5");
const dot6 = document.getElementById("dot6");
function rollDice() {
let roll = Math.floor(Math.random() * 6) + 1;
for (let i = 0; i < refresh.length; i++){
const refreshing = refresh[i];
refreshing.classList.add("hidden");
}
if (roll === 1){
centerDot.classList.remove("hidden");
} else if (roll === 2){
dot1.classList.remove("hidden");
dot6.classList.remove("hidden");
} else if (roll === 3){
centerDot.classList.remove("hidden");
dot1.classList.remove("hidden");
dot6.classList.remove("hidden");
} else if (roll === 4){
dot1.classList.remove("hidden");
dot3.classList.remove("hidden");
dot4.classList.remove("hidden");
dot6.classList.remove("hidden");
} else if (roll === 5){
centerDot.classList.remove("hidden");
dot1.classList.remove("hidden");
dot3.classList.remove("hidden");
dot4.classList.remove("hidden");
dot6.classList.remove("hidden");
} else {
dot1.classList.remove("hidden");
dot2.classList.remove("hidden");
dot3.classList.remove("hidden");
dot4.classList.remove("hidden");
dot5.classList.remove("hidden");
dot6.classList.remove("hidden");
}
}
document.getElementById("roll").addEventListener("click", rollDice);
/*
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?
*/