I have an array of JavaScript strings, and I want to pick a random string from the array. However, when it runs in Edge and Chrome, it doesn't throw any errors, but it picks the same strings from the array every time. I have looked at other answers on Stack Overflow, but none of them seemed to help. Here is my code:
var arr = ["string1, "string2", "string3", "string4", "string5", "string6", "string7"]; /* 100 quotes in the real array. */
var dis = parseInt(prompt("Enter the number of strings you would like to display."));
if(dis > arr.length) {
alert("You have entered a number that is too great.");
} else {
for(var n = 1; n <= dis; n++) {
document.write(arr[(Math.random() * (arr.length - 1))] + "<br/>");
}
}
Anyone have any code snippets that work for making sure it doesn't pick the same strings?
Aucun commentaire:
Enregistrer un commentaire