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>