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
by
AboutCommentsNotes
Solution for day 8 of #javascriptmas
by
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 wold 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()

*/

let diceDots = document.querySelectorAll(".dot");
let diceContainer = document.querySelector(".dice");
let diceArray = Array.from(diceDots);
let numberDisplay =document.querySelector(".number")
diceArray[4].classList.add("dot-style");

const removeClass = () => {
diceArray.forEach((dot) => {
dot.classList.remove("dot-style");
});
};

const randomiser = () => {
removeClass();
let randomise = Math.floor(Math.random() * 6 + 1);
console.log(randomise);

randomise === 1
? diceArray[4].classList.add("dot-style") : 0;

randomise === 2
? (diceArray[2].classList.add("dot-style"),
diceArray[6].classList.add("dot-style"))
: 0;

randomise === 3
? (diceArray[0].classList.add("dot-style"),
diceArray[4].classList.add("dot-style"),
diceArray[8].classList.add("dot-style"))
: 0;

randomise === 4
? (diceArray[0].classList.add("dot-style"),
diceArray[2].classList.add("dot-style"),
diceArray[6].classList.add("dot-style"),
diceArray[8].classList.add("dot-style"))
: 0;

randomise === 5
? (diceArray[0].classList.add("dot-style"),
diceArray[2].classList.add("dot-style"),
diceArray[4].classList.add("dot-style"),
diceArray[6].classList.add("dot-style"),
diceArray[8].classList.add("dot-style"))
: 0;

randomise === 6
? (diceArray[0].classList.add("dot-style"),
diceArray[1].classList.add("dot-style"),
diceArray[2].classList.add("dot-style"),
diceArray[6].classList.add("dot-style"),
diceArray[7].classList.add("dot-style"),
diceArray[8].classList.add("dot-style"))
: 0;
numberDisplay.innerText=`The number is ${randomise}`
};

diceContainer.addEventListener("click", () => {
randomiser();
});


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