0

I am trying to get a random number from an array without repeating but using var newx = Math.floor(Math.random() * array.length); does randomize but it goes by length and not what is inside the array so it does tend to repeat.

<html>
    <head>

    </head>
    <!-- <body> -->
        <button onclick="myFunction()">Difficulty</button>
        <p id="demo">here</p>
        <p id="test"></p>
        <script>



function myFunction() {

      function shuffle(o){ //try this shuffle function
          for(var j, g, t = o.length; t; j = Math.floor(Math.random() * t), g = o[--t], o[t] = o[j], o[j] = g);
          return o;
      };

    var i;    var l;    var y;    var n;
    var newx;   var newy;
    var useranswer;   var amountX;    var largest;
    var copyAmountX;
    var mixAlot;

    var x = parseInt(prompt("What is the first number?"+" "+"(up to 12)"));

    if (x < 13) {
       y = prompt("what is the second number?"+" "+"(choose up to 12)");
       n = prompt("how many problems to be solved?");
       amountX = []

       // adds to an array equal to x (user input)
       if (!amountX.length) {
        for (var s = 0; s <= x; s++) {
            amountX.push(s);
        }
       };
       // just to let me know if it is working. Will be taken out.
       alert(amountX);
        largest = Math.max.apply(Math, amountX);
       alert(largest);
       alert(isNaN(x));
    }
    else {
        alert("Refresh page and restart with numbers under 12")
    };
    if (y > 12 == true) {
        alert("Refresh page and restart with numbers under 12")
    };

    i = 0;
    l = amountX.length;
    copyAmountX = amountX;
    // where the core magic of everything happens.
    while (x < 13 && y < 13 && i<n) {
      newx = shuffle(copyAmountX);
      newy = Math.floor(Math.random() * y);
      useranswer = prompt("Multiply "+newx+" by "+newy)
        if (useranswer == newx * newy) {
            alert("Correct! problem "+(i+1)+" of "+n);
        };
        if (amountX == 0) {alert("You have completed your Difficulty! Good Game"); n = 0;
        };
        i++;
    };
};
</script>


    </body>
</html>
Hunter84
  • 13
  • 1
  • 5

1 Answers1

5

If you are trying to get random numbers from an array, then I would recommend a different method: Copy the array, and shuffle the copy.

function shuffle(o){ //try this shuffle function
    for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
};

This way, you can just keep popping elements from the shuffled array; they will always be random and only ever occur once.


And now about your code, I'm afraid it has a lot of bugs: ... I removed this part

Fix:

I corrected your program and it works perfectly now. Please go through it and apply the changes I commented in the javascript. Link

zoran404
  • 1,682
  • 2
  • 20
  • 37
  • How would i add this to my script? – Hunter84 Sep 28 '14 at 04:52
  • Please read the part I just added about the bugs in your code. – zoran404 Sep 28 '14 at 11:29
  • I updated it, but it doesnt work now. Also I did go through the javascript course on codecademy.com and am working on teamtreehouse.com currently. I am trying to go over everything. – Hunter84 Sep 28 '14 at 18:50
  • It doesn't work because you made some mistakes when you edited your code. Read my new edit. (I might be offline for a while, so if your code doesn't work again just go through it few times, comment parts of your code, and after few tries you should be able to figure it out) – zoran404 Sep 28 '14 at 20:47
  • I updated the code. It is looking better but instead of 1 number as the random outcome, it gives for example : 1,4,3,0,2 by 1 as a math problem. – Hunter84 Sep 29 '14 at 04:47
  • you should learn more about how array work. do to the link I give you in my last edit and apply the changes I marked in your code. I tested it and it works now – zoran404 Sep 30 '14 at 14:35
  • alright I have it working, Thanks for all of your help. I have learned a good bit from your advice and just ask one last thing. Could you break down the shuffle function for me so i can understand it better? Thanks again, Hunter. – Hunter84 Oct 01 '14 at 18:32