I have two variables and I would like to add them randomly in a function, I do with math.random but the problem is that the value is sometimes the same, while I would like it to play between them two values, if it gives the first then the next time it will be the second and vice versa ... I hope I explained it well. Thank you.
const btn = document.getElementById('button')
function random(){
var variable = 2
var arr = [];
var tabprenom=new Array('prenom 1','prenom 2')
while(arr.length < 2){
var r = Math.floor(Math.random() * variable-1) + 1;
if(arr.indexOf(r) === -1) arr.push(r);
}
console.log(tabprenom[r]);
}
btn.addEventListener('click',random)
<button id="button"> Generer aleatoire unique </button>