How do I get a function to repeat itself [x] many times?

Frankie

I'm working on a dice rolling program.

Everything has worked so far until I got to this

Code so far:

function randomNum(max) {
    return Math.floor(Math.random() * (max - 1 + 1)) + 1;
}

var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];

function rollDice(number, sides) {
    return("Rolled "+ names[number] +" "+ names[sides] +" sided dice");
}

function randomDice(number, sides) {
    function repeat(number) {
        randomNum(sides);
        if(sides > 20) sides==20;
}}

The problematic bit is this:

function randomDice(number, sides) {
    function repeat(number) {
        randomNum(sides);
        if(sides > 20) sides==20;
}}

What I want is to say something like

randomDice(2, 20)

which should roll 2 twenty sided dice, but I can't get my randomNum function to repeat itself two times if I want it to roll multiple times.

EDIT: The problem has been answered, thank you everybody for helping :)

Sunny Patel

Simply change out your function repeat(number) declaration to a for-loop: for( var n = 0; n < number; n++). This will get the repetition you desire:

function randomNum(max) {
  return Math.floor(Math.random() * max) + 1;  // No reason to do max+1-1
}

var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen', 'twenty'];

function rollDice(number, sides) {
  return "Rolled " + names[number] + " " + names[sides] + " sided dice";
}

function randomDice(number, sides) {
  var results = []  // Create array to store results
  if (sides > 20)  // Limit sides, but only needs to be done once (outside loop)
    sides == 20;
  for (var n = 0; n < number; n++) {
    var name = names[randomNum(sides)]; // Translate the number to a word
    results.push(name);    // Add the word to the array
  }
  console.log(rollDice(number, sides)); // Display the context
  return results; // Return the results as an array
}

console.log(randomDice(2, 20).join(', '));  // Display the values chosen as comma-separated values

I added a few more things, assuming this is the type of implementation you are looking for. Please adjust as necessary.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I get a function to repeat itself 5 times within another function? (Rock Paper Scissors)

From Dev

How many times do I have to repeat a specific shuffle of playing cards to get back to where I started?

From Dev

How do i repeat my function multiple times with updated values?

From Dev

How to not repeat == many times

From Dev

How do you control the number of times a functions calls, in a recursive function. I.e the number of times the recursive function calls on itself

From Dev

how do I count the amount of times a (recursive) function executes itself in ocaml?

From Dev

How do I get a SKAction to repeat itself after a random period of time

From Dev

How do you get a python script to repeat x times over again?

From Dev

How do I write a function that returns itself?

From Dev

How do I get my while loop to store its result in an array, and repeat 10 times?

From Dev

How do I get a count of how many times a certain number is inside of a json file using php?

From Dev

How do I get event handler to keep track of how many times the user clicked it?

From Dev

How to repeat x, n times?

From Dev

No matter how many times I try re-installing Linux Mint, I get a GRUB error. How do I fix it?

From Dev

How to repeat a function with the values returned by the function itself?

From Dev

How do I repeat my code 12 times with a loop?

From Dev

How do I repeat an Azure Logic App Action 50 times?

From Dev

How to repeat a function five times?

From Dev

How do I repeat this search function in R?

From Dev

How do I repeat a hover function?

From Dev

How do I stop and repeat a function?

From Dev

How do I not repeat function with setTimeout with if condition?

From Dev

Check how many times a pattern does not repeat

From Dev

How do I get the program to repeat itself after the total is printed? (Like a new person is ordering) I know I have to use a loop but don't know how

From Dev

somehow I need to update all the functions so that I can get how many times each function is called during the program lifecycle

From Dev

How can i execute a function many times while press a button?

From Dev

How can excute my function as many times as I want?

From Dev

How do I get this JavaScript to repeat

From Dev

Javascript repeat function x times for y seconds

Related Related

  1. 1

    How can I get a function to repeat itself 5 times within another function? (Rock Paper Scissors)

  2. 2

    How many times do I have to repeat a specific shuffle of playing cards to get back to where I started?

  3. 3

    How do i repeat my function multiple times with updated values?

  4. 4

    How to not repeat == many times

  5. 5

    How do you control the number of times a functions calls, in a recursive function. I.e the number of times the recursive function calls on itself

  6. 6

    how do I count the amount of times a (recursive) function executes itself in ocaml?

  7. 7

    How do I get a SKAction to repeat itself after a random period of time

  8. 8

    How do you get a python script to repeat x times over again?

  9. 9

    How do I write a function that returns itself?

  10. 10

    How do I get my while loop to store its result in an array, and repeat 10 times?

  11. 11

    How do I get a count of how many times a certain number is inside of a json file using php?

  12. 12

    How do I get event handler to keep track of how many times the user clicked it?

  13. 13

    How to repeat x, n times?

  14. 14

    No matter how many times I try re-installing Linux Mint, I get a GRUB error. How do I fix it?

  15. 15

    How to repeat a function with the values returned by the function itself?

  16. 16

    How do I repeat my code 12 times with a loop?

  17. 17

    How do I repeat an Azure Logic App Action 50 times?

  18. 18

    How to repeat a function five times?

  19. 19

    How do I repeat this search function in R?

  20. 20

    How do I repeat a hover function?

  21. 21

    How do I stop and repeat a function?

  22. 22

    How do I not repeat function with setTimeout with if condition?

  23. 23

    Check how many times a pattern does not repeat

  24. 24

    How do I get the program to repeat itself after the total is printed? (Like a new person is ordering) I know I have to use a loop but don't know how

  25. 25

    somehow I need to update all the functions so that I can get how many times each function is called during the program lifecycle

  26. 26

    How can i execute a function many times while press a button?

  27. 27

    How can excute my function as many times as I want?

  28. 28

    How do I get this JavaScript to repeat

  29. 29

    Javascript repeat function x times for y seconds

HotTag

Archive