Mind blown. Thank you all! I'm just now getting into ES6 and shorthand functions etc. I just love how there are ways to do in 10 lines what I did in ~50-60 lines. Incredible.
I'll be referencing this when I update the code in the next few weeks. Thank you both!
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You don't need to have a function that returns another function. You can simplify it to this:
And use it with this:
That's true, but his code wants to call functions directly.
Also, why are you adding 1 and then flooring?
I am adding one because
Math.random()
returns a float between 0 and 1, but it never includes 1, so you could never get 20 without adding 1.Why not use Math.ceil?
Because some dice don't have zero while other's do. So we would need to switch on different dice.
But you're always adding one, so you'll never get zero ...
Exactly. We would remove that but we would need to switch between
Math.ceil
andMath.floor
.Making a function more complex in order to make it simpler to do something the function does not do is not very sensible. :)
What if we just did something like this:
If you want to generate integers in a range, you might as well just do that.
But the first thing to check are the requirements for this use case.
I believe that none of these dice include zero. :)
Mind blown. Thank you all! I'm just now getting into ES6 and shorthand functions etc. I just love how there are ways to do in 10 lines what I did in ~50-60 lines. Incredible.
I'll be referencing this when I update the code in the next few weeks. Thank you both!