I'm new to javascript and trying to get comfy with Functions
, For
loops, and If
statements. I'm working on a simple exercise that generates 5 random numbers from a function call. One bit of logic that I'm struggling with putting together is comparing the numbers created by Math.random that are pushed into an array. What's the best method to use to ensure that all numbers to push to the array are unique (no duplicates)? Would I add an If
statement in the For
codeblock that checks every number, and if they match, rerun the Math.random function? Trying to figure out if that's the best way to approach the problem.
function randoNumbers(min, max){
let randomNumbers = [];
for (let counter = 0; counter < 5 ; counter++){
randomNumbers.push(Math.floor(Math.random() * (max - min) + +min));
}
console.log(randomNumbers);
}
randoNumbers(1, 10);