arrays - Dice Roll Counter in Javascript - Stack Overflow

I am trying to make a dice roll counter in Javascript, but I can't seem to get it to work. I need

I am trying to make a dice roll counter in Javascript, but I can't seem to get it to work. I need the code to roll the dice 25 times. I need to generate a random array of “one” , “two” , “three” , “four” , “five” , “six”. I also need to count how many times each number is rolled. Here's what I have:

function rnd(dice){
var number=Math.floor(Math.random(dice)*25);
}

var dice = ["One" , "Two" , "Three" , "Four" , "Five" , "Six"];

length = dice.length;
console.log(length);

for (var i = 0; i < dice.length; i++) {
console.log("i:" + i + "" + dice[i]);
}

I am trying to make a dice roll counter in Javascript, but I can't seem to get it to work. I need the code to roll the dice 25 times. I need to generate a random array of “one” , “two” , “three” , “four” , “five” , “six”. I also need to count how many times each number is rolled. Here's what I have:

function rnd(dice){
var number=Math.floor(Math.random(dice)*25);
}

var dice = ["One" , "Two" , "Three" , "Four" , "Five" , "Six"];

length = dice.length;
console.log(length);

for (var i = 0; i < dice.length; i++) {
console.log("i:" + i + "" + dice[i]);
}
Share Improve this question asked Sep 27, 2017 at 3:04 Pavus the PugPavus the Pug 231 silver badge5 bronze badges 5
  • Are you looking for someone to just answer this, or to explain the mistakes with your current code? – fubar Commented Sep 27, 2017 at 3:09
  • 1 Study the documentation for Math.random carefully and note that there is no parameter. Then consider carefully what it would mean to try to multiple a random number by 25. Now think carefully about why, if you wanted to throw the dice 25 times, you would write a loop going from 1 to 6. In other words, actually think about your problem instead of randomly throwing code at the wall and wondering why it doesn't work. – user663031 Commented Sep 27, 2017 at 3:11
  • @torazaburo - there's some useful feedback in your ment, but I think OP deserves some credit for actually attempting to solve their own problem, before asking for help. Yes, their work has errors, but the barebones are there. It just needs tweaking and for OP to understand the reason for those changes. OP +1 for effort. – fubar Commented Sep 27, 2017 at 3:14
  • @fubar I am new to code, so having it explained would really help. I just want to learn. – Pavus the Pug Commented Sep 27, 2017 at 3:46
  • @PavusthePug - torazaburo has written a really prehensive explanation. Give that a read and see what you understand. – fubar Commented Sep 27, 2017 at 4:28
Add a ment  | 

3 Answers 3

Reset to default 3

You seem to be subscribing to the "Frankenstein's monster" school of puter programming, which calls for acquiring body parts (code fragments) from somewhere, sewing them together, and then hoping they start to function as a human being (or in this case, as a puter program).

Start at the other end--think top down. Write down, in plain English, what you hope your program will do. Actually, you've already done a lot of that:

I need the code to roll the dice 25 times.

Before you do anything else, write down this part of the problem in the form of pseudo-code, such as:

repeat 25 times
  roll die

Then your problem statement says:

I need to generate a random array of “one” , “two” , “three” , “four” , “five” , “six”.

Here you need to think more carefully about what you are saying you want to do. Look at each word in the sentence. I don't think you really mean "a random array". I think you mean a random value from an array. We'll include that in our pseudo-code, so it now looks like

repeat 25 times
  roll die

to roll die
  generate random value from array of "one" to "six"

Finally,

I also need to count how many times each number is rolled. Here's what I have:

We can address this two ways. One is to add a separate step, which might be called "count rolls". The other is to count the rolls as we go along. The latter seems a little bit simpler. So now our pseudo-code looks like this:

initialize counts

loop from 1 to 25
  roll die
  update counts

to roll die
  generate random value from array of "one" to "six"

The advantage of writing things down this way is that we can see clearly whether it is going to do what we want or not, without getting confused about JavaScript syntax details. We could talk through it to ourselves, using the "rubber ducky" strategy. We could also give it to someone else to implement. Or, we could even implement it in some other language.

However, we are going to implement it in JavaScript. Before we do that, however, we are going to flesh it out a little bit more. It will still be pseudo-code, but a little more detailed. We also need to add some logic to keep our results, so we can print them out later.

define counts as an array of six numbers, with each value initialized to zero
define results as an empty array

loop from 1 to 25
  roll die, getting a result
  update counts based on result
  add results to array

printout results
printout counts

to roll die
  generate random value from array of "one" to "six"
  return that value 

Now and only now we can confidently write a JavaScript program to implement this. Our notion of "to do something" we will implement as a JavaScript function.

function rollDie() {
  return Math.floor(Math.random() * 6); 
}

var counts = [0, 0, 0, 0, 0, 0];

var results = [];

for (var i = 0; i < 25; i++) {
  var result = rollDie();
  counts[result] = counts[result] + 1;
  results.push(result);
}

console.log(results);  
console.log(counts);    

For simplicity, this code uses numbers from zero to five instead of strings from "One" to "Six", but the concept is the same.

It's better to break this problem into several parts.

First, make sure you can roll one die.

const sides = ["One" , "Two" , "Three" , "Four" , "Five" , "Six"];
const roll = () => sides[Math.floor(Math.random() * 6)]

Then roll the die multiple times to get a series of n rolls.

const rolls = (n) => {
    const result = [];
    for (let i = 0; i < n; i++) {
        result.push(roll());
    }
    return result;
};

Finally, count the number of times a given roll appears.

const count = (rolls) => {
    const result = new Map();
    for (const roll of rolls) {
        result.set(
            roll,
            1 + (result.get(roll) || 0));
    }
    return result;
}

I'm very reluctant to answer this question but I do.
First look what is wrong in the OP code.

function rnd(dice){//the function is never used
var number=Math.floor(Math.random(dice)*25);//
}

var dice = ["One" , "Two" , "Three" , "Four" , "Five" , "Six"];

length = dice.length;
console.log(length);//good idea to count

for (var i = 0; i < dice.length; i++) {
console.log("i:" + i + "" + dice[i]);//what do you mean
}

There is no attempt to solve the problem in OP. And the solution es from the OP.

var dice = ["One", "Two", "Three", "Four", "Five", "Six"];//here is a list of names
var stat = {};//here we will collect statistics of dice roll count
for (var i = 0; i < 25; ++i) {//"magic" 25 is the number of rolls
  var rand = Math.floor(Math.random() * 6);//roll to have random [0,5]; if "dice" has more sides then use dice.length instead of "magic" 6
  stat[dice[rand]] = (stat[dice[rand]] || 0) + 1;//now count hits
}
console.log(stat);

I hope you can explain how your homework is done.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744905780a4600243.html

相关推荐

  • arrays - Dice Roll Counter in Javascript - Stack Overflow

    I am trying to make a dice roll counter in Javascript, but I can't seem to get it to work. I need

    1天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信