2

Background

I'm just learning JavaScript and I thought that a good way to start was to code my own dice roller for an RPG I play. For this game you roll a handful of six-sided dice where each 4 or 5 is worth one success and each 6 is worth 2 successes.

I have a function that rolls the dice and passes the rolls, via an array, to another function that HTML-ifies it for display on the webpage.

The Code

In order to roll the dice, I have what I think is the standard dice roller function (the sides variable is because sometimes you can roll dice that have sides other than 6, so I made the function usable for all dice rolls):

function rollDice(sides) {
  return Math.floor(Math.random()*sides) + 1;
}

Because the number of dice you roll varies, my main function calls this rollDice() function a number of times and puts each result in an array.

This is the main function, where number is the number of dice to roll:

function dice(number) {
  let results = [];
  for (i=0; i<number; i++){
    results.push(rollDice(6));
  }
  return results;
}

However, I have since learned of the do…while loop, and have managed to refactor it thusly:

function dice(number) {
  let results = [];
  do {
    results.push(rollDice(6));
  } while (results.length < number)
  return results;
}

The Actual Question

Which loop is more 'correct' or best practice to use? for or do…while?

Christopher
  • 123
  • 4
  • `for` loops are definitely more common, and I wouldn't look twice at the first example. But as you've discovered, `do...while` loops can in some situations provide a very pleasing solution! I wouldn't say either of these is strictly more correct than the other – Hamms Sep 19 '18 at 01:12
  • 2
    Consider, however, what you want to happen when `number` is less than one. – Hamms Sep 19 '18 at 01:13
  • Depends on your requirements. Do... While loop run at least one time if condition not match when for loop runs only if condition match – Hitesh Kansagara Sep 19 '18 at 01:13
  • if `number` never touches 0 or below, I think the `do... while` will do. but I really rarely see `do... while` in my own experience. – Wreigh Sep 19 '18 at 01:14
  • 1
    I agree with everyone who says thy rarely see do...while in the wild although there's nothing necessarily WRONG with using it in the right situation. I will add, though, that in school I was discouraged from using it for reasons similar to what was outlined in this question: https://stackoverflow.com/questions/994905/is-there-ever-a-need-for-a-do-while-loop . That's a bit subjective, however :) – Rose Robertson Sep 19 '18 at 01:17
  • If you haven't researched it yet you may also be interested in `forEach` https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – Rose Robertson Sep 19 '18 at 01:20
  • Since you’re always rolling at least one die, `number` will never be less than 1. – Christopher Sep 19 '18 at 09:04

1 Answers1

1

You can just use a simple while loop which is equivalent to your for loop example.

function dice(number) {
  let results = [];
  while (results.length < number) {
    results.push(rollDice(6));
  }
  return results;
}

I like the readability of this the best (partly because it doesn't create an extra iterator variable), but the for loop is fine too. The do...while loop works but the extra syntax is unnecessary.

Maximillian Laumeister
  • 19,884
  • 8
  • 59
  • 78