I have numbers array, through my_number function I create a random numer, if it already exist in the array, I call the function again and create a new random number, if it is not in the array I send it to it through push.
Unfortunately it sems to send all numbers again and again until browser crashes.
How could I fix this.
Thanks for your help
var numbers = [1,2,3,4,5];
function mainFunction(){
function my_number(){
var randomNumber = Math.floor((Math.random()*7)+1);
for(let i=0; i<numbers.length; i++){
if(numbers[i] === randomNumber){
my_number();
}else{
numbers.push(randomNumber);
}
}
}
my_number();
}
mainFunction();
numbers.lengthchanges.i, notnumbers[i].numbers.includes(randomNumber). Theincludes()method determines whether an array includes a certain value among its entries, returningtrueorfalseas appropriate.