I'm just trying to generate 8 random numbers from 1 to 25. My issue is that I already have a variable and its value is 14, with that being said my question is how to generate random numbers from 1 to 25 and if one them is equal to 14 then replace it with another random number? at the end, I want to have 8 random numbers in my array and I don't want to include 14 in it. Can anyone tell me what I'm missing or how to make this more accurate? and sometimes I only end up with 7 elements in my array, does anyone knows why?
Here's my code:
var arr = [];
var currentvar = 14;
for(var i = 0 ; i < 9; i++){
var num = Math.floor(Math.random()*25) + 1;
if(num === currentvar){
num = Math.floor(Math.random()*25) + 1;
}else{
arr.push(num);
}
}
console.log(arr);