Question:
I am making a Dice Roller program to see how long it takes to roll an amount of dice with an amount of sides. To start a timer I am using the performance.now()
function but it doesn’t work, it says it’s not a function.
Repl Link:
https://replit.com/@SalladShooter/Die-Roller#index.js
var performance = require("performance");
function randInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min
}
let dieAmount = prompt("Amount of Dice ")
let dieSides = prompt("Amount of Sides on a Die ")
dieSides = parseInt(dieSides)
dieAmount = parseInt(dieAmount)
let rolledCorrectly = false
let startTime = performance.now()
while (!rolledCorrectly) {
const rolls = []
for (let i = 0; i < dieAmount; i++) {
let currentRoll = randInt(1, dieSides)
rolls.push(currentRoll)
}
let allSame = true;
for (let j = 1; j < rolls.length; j++) {
if (rolls[0] !== rolls[j]) {
allSame = false
break
}
}
if (allSame) {
rolledCorrectly = true
}
}
let endTime = performance.now()
let totalTime = (startTime - endTime) / 1000
console.log('Successfully rolled the same number on all dice! It took ${totalTime} seconds')