I need to create a array with unique values. In here if the created value is include in that array, then need to create another value and again need to check that newly created value exists in that array, if again exists then need to do the same check.
Here is the code I have tried, if I execute this, I think infinite looping scene happening.
let arr = [];
for(let i=0; i<10;i++) {
let k = 1;
let pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';
while(k > 0){
k++;
if(arr.indexOf(pass) > -1) {
pass = (Math.floor(Math.random() * (10 - 6 + 1)) + 6)+'a';
} else {
arr.push(pass);
break;
}
console.log(arr)
}
}
What was the mistake in this code?
(Math.floor(Math.random() * (10 - 6 + 1)) + 6)will get you a number from 6 to 10 if I am not mistaken, second of all how would you want 10 unique items in an array if you generate on 5 unique ? Third of all, the problem is herek > 0it goes in an infinite loop.