-1

I am looking to get the results of the number of each number that shows in the results.

So if they request for 10 random numbers they can see their numbers and how many of each number there is. Somehow I need the [1, 2, 3, 4, 5, 2, 3, ] to pull in the results..

https://jsfiddle.net/by62764z/6/

function addFields() {
    var number = document.getElementById("Rando").value;
    var container = document.getElementById("container");


    while (container.hasChildNodes()) {
        container.removeChild(container.lastChild);
    }
    for (i = 0; i < number; i++) {
        //move arr here
        var Numbro = Math.floor((Math.random() * 10) + 1);
        container.appendChild(document.createTextNode(" " + (Numbro)));
        var input = document.createElement("input");
        input.type = "number";
        container.appendChild(document.createElement("br"));



    }



}


var dataset = [1, 2, 3, 4, 5, 2, 3, ];

function findOccurrences(arr, val) {

    var i, j,
        count = 0;
    for (i = 0, j = arr.length; i < j; i++) {
        (arr[i] === val) && count++;
    }
    return count;
}

document.write("one " + findOccurrences(dataset, 1) + "<br>");
document.write("Two " + findOccurrences(dataset, 2) + "<br>");
document.write("Three " + findOccurrences(dataset, 3) + "<br>");
document.write("Four " + findOccurrences(dataset, 4) + "<br>");
document.write("Five " + findOccurrences(dataset, 5) + "<br>");
document.write("Six " + findOccurrences(dataset, 6) + "<br>");
document.write("Seven " + findOccurrences(dataset, 7) + "<br>");
document.write("Eight " + findOccurrences(dataset, 8) + "<br>");
document.write("Nine " + findOccurrences(dataset, 9) + "<br>");
document.write("Ten " + findOccurrences(dataset, 10) + "<br>");
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Andrewp
  • 25
  • 3
  • 2
    So what exactly does not work? Where is `addFields` called? – Bergi Feb 10 '15 at 16:09
  • The `j` variable is entirely unnecessary BTW. And this is just plain ugly: `(arr[i] === val) && count++;`, `if (arr[i] === val) {count++}` is much clearer. – meskobalazs Feb 10 '15 at 16:12
  • I am sorry I am very new to this. When the user types a number in the input it generates that many random numbers. I am then trying to display how many of each number is generated. – Andrewp Feb 10 '15 at 16:15
  • And, what is the problem you are facing? – meskobalazs Feb 10 '15 at 16:15
  • @meskobalazs No, it is not 'entirely' unnecessary, it is the most basic loop optimization. – laruiss Feb 10 '15 at 16:18
  • What exactly you want? – Mukesh Agarwal Feb 10 '15 at 16:28
  • If you see the fiddle you can generate x number of random numbers (1-10) . I want to be able to also display how many of each number 1-10 are randomly picked. – Andrewp Feb 10 '15 at 16:30
  • @laruiss I assumed any sane browser would optimize this, it seems – meskobalazs Feb 10 '15 at 17:09
  • possible duplicate of [JavaScript - Count duplicates within an Array of Objects](http://stackoverflow.com/questions/10541068/javascript-count-duplicates-within-an-array-of-objects) – user2864740 Feb 10 '15 at 18:04

1 Answers1

0

It's very very very ugly and did fast, but this works (if i got what you wanted).

function addFields() {
  var number = document.getElementById("Rando").value;
  var container = document.getElementById("container");
  var range = 10;    
  var nb = new Array(range);

  while (container.hasChildNodes()) {
    container.removeChild(container.lastChild);
  }

    var numberPicked = "Number Picked : ";
  for (i = 0; i < number; i++) {
    var Numbro = Math.floor((Math.random() * range)+1);
      numberPicked += " "+Numbro+" ; "
      if(nb[Numbro] == undefined)
          nb[Numbro] = 0;
      nb[Numbro]++;
  }

    var textNode = document.createTextNode(numberPicked) ;
    container.appendChild(textNode);
    container.appendChild(document.createElement("br"));

  for  (i = 0; i < nb.length; i++) {
    var textNode = document.createTextNode("number of  "+i+ " : " + (nb[i] || 0 )) ;
    container.appendChild(textNode);
    container.appendChild(document.createElement("br"));
  }
}

if what i did is what you want, i can help you make it much better ^^

https://jsfiddle.net/boj93dct/

Crocsx
  • 2,534
  • 1
  • 28
  • 50
  • Thank you for your help but it does not seem to be generating the amount of random numbers that is typed in the field OR the random number any longer. – Andrewp Feb 10 '15 at 16:47
  • it will sort the the amount of random number that you have added in the input, and it will take the random number beetwen 0 and range. I write 15 in the inpute and i have range = 10, i will have 15 random number beetwen 0 and 10. The only thing is that it will stop on the last number sorted, so if you have 0 times 9 it won't show 9 ^^ I can fix it just a sec – Crocsx Feb 10 '15 at 16:55
  • I do apologize as I am very new to learning. This seems to work perfect. But I do not see where it displays the random numbers – Andrewp Feb 10 '15 at 17:11
  • You mean in the code or? If this show you nothing on jsFiddle, is bcause you have to click on " Number of Numbers: " (like you did above in your js Fiddle) If it's in the code, i display the number by adding them in a paragraph with createTextNode that is appendChild to your container (the second for) – Crocsx Feb 10 '15 at 17:12
  • well you writed in your code that the function is called onclick of an link element () so you have to click on your link to call the method addFields Look the jsfiddle i linked above, and look on the internet about "link in html" and "onclick" – Crocsx Feb 10 '15 at 17:16
  • well i don't get what you want so ^^ Actually it pick x random number beetween 0 and range, and show you for each number in the range, how many time this number as been randomly picked. What did you need more than that? :) – Crocsx Feb 10 '15 at 17:23
  • What you did was great and I thank you. I just don't see the random numbers. I only see how many of each number there are. I want to see both. – Andrewp Feb 10 '15 at 17:24