I am trying to generate an array using a loop with each value in the array being unique. Specifically, I would like to have five elements in the array, with each being a random number between 1 and 100 (without duplicates).
Is this the right way to do it?
P.S I am a beginner with only 8 days of JS experience.
Thanks in advance!
let arr = []
let num = 0
let x = 4
while (x >= 0) {
if (arr.indexOf(arr[x]) == arr.lastIndexOf(arr[x])){
arr.push(Math.floor(Math.random(num)*100))
} else if (arr.indexOf(arr[x]) !== arr.lastIndexOf(arr[x])) {
arr.push(Math.floor(Math.random(num)*100))
} else {
arr.push(Math.floor(Math.random(num)*100))
}
x--
}
console.log(arr)