scrimba
Solution for day 8 of #javascriptmas
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

Solution for day 8 of #javascriptmas
AboutCommentsNotes
Solution for day 8 of #javascriptmas
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 👇
const dot_1 = document.getElementById("dot-1")
const dot_4 = document.getElementById("dot-4")
const dot_7 = document.getElementById("dot-7")

const dot_2 = document.getElementById("dot-2")
const dot_5 = document.getElementById("dot-5")
const dot_8 = document.getElementById("dot-8")

const dot_3 = document.getElementById("dot-3")
const dot_6 = document.getElementById("dot-6")
const dot_9 = document.getElementById("dot-9")

const dice = document.getElementsByClassName("dice")
dice[0].addEventListener("click", rollDice)

var dice_value = document.getElementById("dice-value")


function rollDice(){
dot_1.classList.add("hide")
dot_4.classList.add("hide")
dot_7.classList.add("hide")
dot_2.classList.add("hide")
dot_5.classList.add("hide")
dot_8.classList.add("hide")
dot_3.classList.add("hide")
dot_6.classList.add("hide")
dot_9.classList.add("hide")

var diceValue = Math.floor(Math.random() * 6) + 1
console.log(diceValue)
dice_value.innerHTML = `Rolled value is : ${diceValue}`;

switch(diceValue) {
case 1:
dot_5.classList.remove("hide")
break;
case 2:
dot_1.classList.remove("hide")
dot_9.classList.remove("hide")
break;
case 3:
dot_1.classList.remove("hide")
dot_5.classList.remove("hide")
dot_9.classList.remove("hide")
break;
case 4:
dot_1.classList.remove("hide")
dot_3.classList.remove("hide")
dot_7.classList.remove("hide")
dot_9.classList.remove("hide")
break;
case 5:
dot_1.classList.remove("hide")
dot_3.classList.remove("hide")
dot_5.classList.remove("hide")
dot_7.classList.remove("hide")
dot_9.classList.remove("hide")
break;
case 6:
dot_1.classList.remove("hide")
dot_4.classList.remove("hide")
dot_7.classList.remove("hide")
dot_3.classList.remove("hide")
dot_6.classList.remove("hide")
dot_9.classList.remove("hide")
break;
}
}
/*

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
5
,
4
,
2
,
/index.html
LIVE