I have been trying to figure this out for a while using an IF statement but I no longer think that may be the best way to go and am not sure what to try now.
I have 6 input values that are randomly generated. The first 5 pull a random name from their respective list of names, the 6th pulls a random name from all of the lists combined. Should this be a separate array containing all the names or is there a way to combine the arrays?
The other issue is that the 6th input needs to keep randomizing until its input value is not equal to any of the first 5.
Please let me know how you would go about checking the 6th value against the other 5 and then keep getting a new value.
Thanks for all the help
function getRandom1()
{
var names = [
'Red1',
'Blue1',
'Green1'
];
return names[Math.floor(Math.random() * names.length)];
}
function getRandom2()
{
var names = [
'Red2',
'Blue2',
'Green2'
];
return names[Math.floor(Math.random() * names.length)];
}
function getRandom3()
{
var names = [
'Red3',
'Blue3',
'Green3'
];
return names[Math.floor(Math.random() * names.length)];
}
function getRandom4()
{
var names = [
'Red4',
'Blue4',
'Green4'
];
return names[Math.floor(Math.random() * names.length)];
}
function getRandom5()
{
var names = [
'Red5',
'Blue5',
'Green5'
];
return names[Math.floor(Math.random() * names.length)];
}
function getRandomX()
{
var names = [
'Red1',
'Blue1',
'Green1',
'Red2',
'Blue2',
'Green2',
'Red3',
'Blue3',
'Green3',
'Red4',
'Blue4',
'Green4',
'Red5',
'Blue5',
'Green5'
];
return names[Math.floor(Math.random() * names.length)];
}
function myFunction() {
document.getElementById("input_1").value =( getRandom1() );
document.getElementById("input_2").value =( getRandom2() );
document.getElementById("input_3").value =( getRandom3() );
document.getElementById("input_4").value =( getRandom4() );
document.getElementById("input_5").value =( getRandom5() );
document.getElementById("input_X").value =( getRandomX() );
}
<body onload="myFunction(), myFunction2()">
<select onchange="myFunction(), myFunction2()" id="select">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
<option value="4"> 4 </option>
<option value="5"> 5 </option></select>
<br>
1 <input type="text" id="input_1"/><br>
2 <input type="text" id="input_2"/><br>
3 <input type="text" id="input_3"/><br>
4 <input type="text" id="input_4"/><br>
5 <input type="text" id="input_5"/><br>
X <input type="text" id="input_X"/>
Aucun commentaire:
Enregistrer un commentaire