Combine the random results of two dice rolls using JavaScript for a specified amount of rolls, "n"

I'm currently working on a project that involves adding up the random rolls of a pair of dice for n rolls as specified by the user. Take a look at the code below:

<!DOCTYPE html>
<html>
    <head> 
        <link rel="stylesheet" type="text/css" href="dice.css">
        <script>
//this function is meant to generate 2 random numbers
function roll() { 
    var x = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
    var y = Math.floor(Math.random() * ((6 - 1) + 1) + 1);
    var Total = x + y;
}

//prompts user to input how many times they wish to roll the two dice
function myinput() { 
    var NumRoll = prompt("Please enter the number of times you wish to roll");

    if  (NumRoll <= 0 ) 
        document.getElementById("wrong").innerHTML = "You entered an invalid number, please enter a value between 1-100";
    else if ( NumRoll >100 )
        document.getElementById("wrong").innerHTML = "You entered an invalid number, please enter a value between 1-100";
    else 
        document.getElementById("right").innerHTML = "Rolling the dice " + NumRoll + " times";
}
        </script>
    </head>
    <body>
        <p id="wrong"> </p>
        <p1 id="right"></p1>
        <p>Click to roll the dice!</p>

        <button onclick="myinput(); roll()">Press to Roll</button>
    </body>
</html>

Answer №1

introduce a new parameter to the roll() function and enclose the variables x and y in a for loop. Next, pass the user-input value to the roll function :)
<input id="numRoll" type="text" placeholder="Number of times to throw the dice">
<br><a href="#" id="rollTrigger">Throw the dice!</a>
<br>
<span id="total"></span>

For more information, please check out the jsfiddle link below:

https://jsfiddle.net/bzdgz6ac/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountered an error while attempting to search for '/user/profile' in the views directory

I am currently working on developing a shopping cart using node.js, express.js, and mongodb. I am focusing on setting up the sign-up functionality at the moment. However, when I attempt to access the localhost:3000/user/profile link, I encounter the follow ...

Comparison between UI Router and ngRoute for building single page applications

Embarking on a new Angular project, a single page app with anticipated complex views such as dialogs, wizards, popups, and loaders. The specific requirements are yet to be clarified. Should I embrace ui.router from the start? Or begin with ngRoute and tra ...

The current date is indicating that the date string provided is invalid for interpretation by dayjs()

Have you tried using DayJs? If you're working on the browser, specifically with Firefox + Vue + typescript, here's my date string: 2021-02-05 12:00 AM However, when I include the AM/PM in the code like this: const dateObj: any = dayjs('2 ...

Tips for effectively handling unique properties of a React component (such as Redux integration and custom CSS) when sharing through NPM distribution

Question: How can custom Redux containers and CSS be effectively managed with NPM? It can be challenging to handle these custom files through traditional package distribution platforms like NPM, especially when they need to be edited in various projects. ...

Trying to assign a property to an undefined variable inside a function

At the moment, I am configuring error messages on a login page for an extension using vue, and encountering issues within the importCreds() function. data(){ return { url:'', apikey:'', error:'', } }, meth ...

Is there a way to copy this JavaScript variable and modify the displayed text?

I am working on a JavaScript code that is used to create a basic news page with a filter from a mySQL database. The code generates the heading, main content, and date created for each news item. I now need to add a "read more" link at the bottom of each ne ...

Having trouble retrieving a JSON result from a method located in a different namespace

In my project, I have organized my code into separate .js files and namespaces for json requests and actual logic. Despite this organization, I am facing issues with retrieving the result back in my logic layer. var jsonResult = Blah.Data.LoadAggregates( ...

Alter the size of an element's width dynamically while maintaining a predetermined width

Having a fixed width div: // CSS @media only screen .column.small-centered:last-child, .columns.small-centered:last-child { float: none; } .lesson_lt_mc_div .small-centered { margin-top: 15px; margin-bottom: 15px; width: 296px; } foundation.css:1 ...

How to retrieve values from checkboxes generated dynamically in php using jquery

This is a unique question and not related to event binding or any suggested duplicates. Hello, I am facing an issue while trying to fetch the value of a checkbox to send it in an ajax request to a PHP page. The checkboxes are dynamically created using PHP ...

Transforming a bezier curve into a flat surface in three.js

I am currently working on creating a curved road in three.js using bezier calculations. However, I am facing a challenge in converting the sequence of curved lines into a curved plane. Within my 3D scene, I have cars, a road made from a plane, and a path ...

While experimenting with p5.js, I encountered an issue where I received the error message stating that "loadAnimation is not defined and createSprite not defined." This problem persists even when using VSCode

let background, sleeping, brushing, gyming, eating, drinking, moving, astronaut; function preload() { background = loadImage("images/iss.png"); sleeping = loadAnimation("images/sleep.png"); brushing = loadAnimation("images/ ...

Is it possible in TypeScript to convert a nested ternary into a standalone statement?

I encountered an error while working with the code snippet provided below. As I am relatively new to nested ternary operations, I would appreciate any assistance you could provide. Here is the example code: get notEmptyProduct(): string[] { return th ...

What is the best way to accomplish this in Next.js?

I am working on a NextJs application and I need to include a new route /cabinet with its own navigation. The challenge is that the application already has a navigation bar defined in _app.js. Is it possible to create a similar setup like _app.js for /cab ...

When navigating between Dynamic Pages using NuxtLink, the store data is not accessible

Check out the demo below. Click here for stackblitz When transitioning from a top page to a post page, the correct content is displayed. However, moving from one post page to another does not display the correct content immediately. Reloading the page w ...

The Challenge of Dynamic Angular Components

Upon triggering the blur event of the contenteditable div, I observe changes and dynamically generate new strings with additional spans to update the same div accordingly. However, a challenge arises when all text is deleted from the contenteditable div, r ...

Experiencing a problem when attempting to activate the html5 mode feature in AngularJS to eliminate the #

I've been scouring through various sources like stackoverflow and the web, but I haven't found a clear answer to my question. Can someone please help me out? Here's what I'm struggling with: In my AngularJS application, I enabled &apos ...

Is there a particular Jquery extension that can achieve this special effect, and how is this effect commonly referred

I have successfully created this interactive field effect using raw jQuery code multiple times, but I am interested in turning it into a plugin. The challenge I am facing is that I do not know the specific name of this effect to search for an existing plug ...

Having a problem with the glitch effect in Javascript - the text is oversized. Any tips on how to resize

I found some interesting code on the internet that creates a glitch text effect. However, when I implement it, the text appears too large for my webpage. I'm struggling to adjust the size. Here is the current display of the text: too big This is how ...

I am looking to remove identical innerText values from two arrays using JavaScript

Is it possible to use JavaScript to remove the "added" className of an element with the same innerText when the delete button (.btn-wrap>li>button) is clicked? <div class="table"> <ul> <li class="added">l ...

Display Page Separation with JavaScript

I am working on a project where I have created payslips using Bootstrap through PHP. To maintain organization, I am looking to create a new page after every 6 payslips. Below is the code snippet that I am using to generate the payslips: foreach($results a ...