scrimba
Note at 0:42
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 0:42
by Hil
AboutCommentsNotes
Note at 0:42
by Hil
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 👇
// clear all classes and append a dot to the divs directly?
const diceEl = document.querySelector(".dice");
let rollText = document.querySelector(".rolltext");
let dot1 = document.querySelector(".d1");
let dot2 = document.querySelector(".d2");
let dot3 = document.querySelector(".d3");
let dot4 = document.querySelector(".d4");
let dot5 = document.querySelector(".d5");
let dot6 = document.querySelector(".d6");
let dot7 = document.querySelector(".d7");
let dot8 = document.querySelector(".d8");
let dot9 = document.querySelector(".d9");
let diceRoll;

function removeDotClass() {
// console.log("removeDotClass");
dot1.classList.remove("dot");
dot2.classList.remove("dot");
dot3.classList.remove("dot");
dot4.classList.remove("dot");
dot5.classList.remove("dot");
dot6.classList.remove("dot");
dot7.classList.remove("dot");
dot8.classList.remove("dot");
dot9.classList.remove("dot");
}

dot5.classList.add("dot");

diceEl.addEventListener("click", async () => {
await removeDotClass();

// console.log("dice has been rolled");
diceRoll = Math.floor(Math.random() * 6) + 1;
// console.log("diceRoll",diceRoll);

if (diceRoll === 1) {
dot5.classList.add("dot");
} else if (diceRoll === 2) {
dot1.classList.add("dot");
dot9.classList.add("dot");
} else if (diceRoll === 3) {
dot1.classList.add("dot");
dot5.classList.add("dot");
dot9.classList.add("dot");
} else if (diceRoll === 4) {
dot1.classList.add("dot");
dot3.classList.add("dot");
dot7.classList.add("dot");
dot9.classList.add("dot");
} else if (diceRoll === 5) {
dot1.classList.add("dot");
dot3.classList.add("dot");
dot5.classList.add("dot");
dot7.classList.add("dot");
dot9.classList.add("dot");
} else if (diceRoll === 6) {
dot1.classList.add("dot");
dot3.classList.add("dot");
dot4.classList.add("dot");
dot6.classList.add("dot");
dot7.classList.add("dot");
dot9.classList.add("dot");
}

rollText.innerHTML = `You rolled a ${diceRoll}!`;
});

/*

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?
*/
Console
/index.html
LIVE