The thing is that I want to obtain random values from an array through a loop until they run out.
All the arrays have 7 values in total and, in the end, what I want to achieve is to obtain a combination of said values automatically to generate sentences in the same way.
What I want is for those colours, countrys and days values to be sorted randomly.
This is the loop used for what it's worth:
var colours = ["White", "Red", "Black", "Purple", "Grey", "Yellow", "Blue"];
var countrys = ["Japan", "Korea", "Spain", "England", "China", "Singapur","Rumania"];
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
for (var n = 0; n < colours.length; n++) {
document.write("My " + (n+1) + " choice is " + colours[n] + " in " + countrys[n] + " on " + days[n] + "<br>");
}
You can determine that when performing the loop it takes random elements from each array, for example:
colours[Math.floor(Math.random() * colours.length)]
, you would get a random element from the arraycolours
.This would be a complete example:
You could create a method that randomizes the arrays and then display them as you are doing.
This way you make sure that you get a combination of all the values, without repeating them.
Instead of selecting the options randomly and controlling that they are not repeated, you could order the arrays randomly (
eg: shuffle
)For this you could use the version of
Fisher–Yates shuffle algorithm
.demonstration:
Another example using prototype