Trying to generate a set of strings such as 'TCP', 'DNS' or 'SQL' I want to be able to have these strings set and be able to have them appear randomly like you would with random numbers.
Asked
Active
Viewed 121 times
-2
-
1Please specify what do you mean by 'set strings'. Are they all three-letter computer terminologies? – KeroppiMomo Mar 23 '19 at 00:25
-
Just use an array and Math.random to choose and index, for example `['TCP', 'DNS', 'SQL'][Math.floor(Math.random()*3)]`. – robinsax Mar 23 '19 at 00:25
-
1Shuffle the array and when adding a question better to add your code snippet. https://stackoverflow.com/questions/6274339/how-can-i-shuffle-an-array – Harshana Mar 23 '19 at 00:31
1 Answers
1
You can make an array and select them randomly:
const strings = ['TCP', 'DNS', 'SQL'];
for (var i = 0; i < 5; i++) {
console.log(`Random string: ${strings[Math.floor(Math.random() * strings.length)]}`);
}

Jack Bashford
- 43,180
- 11
- 50
- 79