<divclass="border-gray"><p><buttontype="button"class="primary">roll the dice</button></p><figure></figure></div>
1 2 3 4
Line 8: Use the formula Math.floor(Math.random() * (max-min+1) + min) to calculate a random integer number between min and max (both boundaries included)
Line 14: we use the slice()(opens new window) method to remove the 2 last characters (+ and a space) from
the string caption
const diceButton = document.querySelector('main button');
diceButton.addEventListener('click',function(e){let dices ='';let caption ='';let sum =0;for(let i =0; i <3; i++){const roll = Math.floor(Math.random()*6+1);
sum += roll;
caption +=`${roll} + `;
dices +=`<img src="/assets/dices/dice${roll}.svg" alt="dice ${roll}">`;}// remove last 2 characters from the string and add the sum
caption =`${caption.slice(0,-2)} = ${sum}`;
document.querySelector('main figure').innerHTML =`${dices}<figcaption><h4>${caption}</h4></figcaption>`;});// dispatch (trigger) click event
diceButton.dispatchEvent(newEvent('click'));