mardi 2 octobre 2018

Randomizing input value if same as another

How do I get an input value to change if it is the same as the value of another input?

I have 6 inputs. The first 5 receive a random value from their own lists, but the 6th input value is randomly selected from all of the 5 lists combined. I want this last input value to check itself against the other 5 value and then make sure it is different. I am not sure if I use the same function or if I should create another one that checks the values. I feel like there is probably a very simple but I am having trouble with it.

Thanks

function getRandomName1()
{
        var names = [
'Red1',
'Blue1',
'Green1',
'Yellow1',
'Orange1',
'Purple1',
'White1',
'Black1'
];
        
        return names[Math.floor(Math.random() * names.length)];
}

function getRandomName2()
{
        var names = [
'Red2',
'Blue2',
'Green2',
'Yellow2',
'Orange2',
'Purple2',
'White2',
'Black2'
        ];
        
        return names[Math.floor(Math.random() * names.length)];
}

function getRandomName3()
{
        var names = [
'Red3',
'Blue3',
'Green3',
'Yellow3',
'Orange3',
'Purple3',
'White3',
'Black3'
        ];
        
        return names[Math.floor(Math.random() * names.length)];
}

function getRandomName4()
{
        var names = [
'Red4',
'Blue4',
'Green4',
'Yellow4',
'Orange4',
'Purple4',
'White4',
'Black4'
        ];
        
        return names[Math.floor(Math.random() * names.length)];
}

function getRandomName5()
{
        var names = [
'Red5',
'Blue5',
'Green5',
'Yellow5',
'Orange5',
'Purple5',
'White5',
'Black5'
];
        
        return names[Math.floor(Math.random() * names.length)];
}

function getRandomNameFLX()
{
        var names = [
  'Red1',
'Blue1',
'Green1',
'Yellow1',
'Orange1',
'Purple1',
'White1',
'Black1',
'Red3',
'Red2',
'Blue2',
'Green2',
'Yellow2',
'Orange2',
'Purple2',
'White2',
'Black2',
'Blue3',
'Green3',
'Yellow3',
'Orange3',
'Purple3',
'White3',
'Black3',
'Red4',
'Blue4',
'Green4',
'Yellow4',
'Orange4',
'Purple4',
'White4',
'Black4',
'Red5',
'Blue5',
'Green5',
'Yellow5',
'Orange5',
'Purple5',
'White5',
'Black5'

];
        
        return names[Math.floor(Math.random() * names.length)];
}




function myFunction() {
    if (document.getElementById("select").value == "1") {
        document.getElementById("input_1").value =( getRandomName1() );
        document.getElementById("input_2").value =( getRandomName2() );
        document.getElementById("input_3").value =( getRandomName3() );
        document.getElementById("input_4").value =( getRandomName4() );
        document.getElementById("input_5").value =( getRandomName5() );
        document.getElementById("input_X").value =( getRandomNameFLX() );
} else {  document.getElementById("input_1").value = "blank";
        document.getElementById("input_2").value = "blank";
        document.getElementById("input_3").value ="blank";
        document.getElementById("input_4").value ="blank";
        document.getElementById("input_5").value ="blank";
        document.getElementById("input_X").value ="blank";
}
}
<body onload="myFunction()">

<select class="form-dropdown" onchange="myFunction()"  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>

      
         1 <input  type="text" id="input_1"/>
         2 <input  type="text" id="input_2"/>
          3 <input  type="text" id="input_3"/>
         4 <input  type="text" id="input_4"/>
         5  <input  type="text" id="input_5"/>
    X <input  type="text" id="input_X"/>
  
  



Aucun commentaire:

Enregistrer un commentaire