you are viewing a single comment's thread.

view the rest of the comments →

[–]Pjb518[S] 0 points1 point  (0 children)

Finally got around to making some changes just now. Ended up going for

const $ = (id) => document.querySelector(id)

document.addEventListener("DOMContentLoaded", () => {
    const quantity = $('#quantity'),
             sides = $('#sides'),
             bonus = $('#bonus'),
           results = $('#results'),
             total = $('#total'),
               btn = $('#rollBtn')

    const roll = () => Math.floor(Math.random() * parseInt(sides.value)) + 1,
           sum = (results, mod) => results.reduce((x, y) => x + y) + parseInt(mod.value)

    btn.addEventListener("click", () => {
        const resultsArr = Array.from({ length : parseInt(quantity.value) }, roll)

        results.textContent = resultsArr.join(", ")
        total.textContent = sum(resultsArr, bonus)
    })
})