jeudi 26 octobre 2017

JavaScript IF statement not recognizing value

function init(){
        document.getElementById("add_soldier_button").onclick = addSoldier;
        document.getElementById("deploy_squad_button").onclick = deploySquad;
}
        function Soldier(allegiance, armored, weapons){
        this.allegiance = allegiance;
        this.weapons = weapons;
        this.armored = armored;
        
        }
        function checkSoldierConstraints(allegiance, armored, weapons){
        
        if(weapons.length == 0)
                return "No weapons specified";
        
        if(armored == "")
                return "Armored not set";
        
        if(allegiance == "")
                return "No allegiance specified";
        
        if (allegiance.value != "khorne" || "slaanesh" || "nurgle" || "tzeentch")
                return "allegiance must be to khorne, slaanesh, nurgle, or tzeentch";
        
        if (weapons.value  != "chainsword" || "plasma cannon" || "bolt rifle" || "magic")
                return "Weapon must be a chainsword, plasma cannon, bolt rifle , or magic";
                
        if ( allegiance.value == "tzeentch" && weapons.value != "magic") 
return " tzeentch can only use magic";          
        
        soldiers.push(new Soldier(allegiance, armored, weapons));
        return "Creating Soldier";
        
        
        
        
        }
        
        var soldiers = new Array();
function addSoldier(){
        
        
        var allegiance_text = document.getElementById("allegiance_input");
        var armored_text = document.getElementById("armored_input");
        var weapon1_text = document.getElementById("weapon1_input");
        var weapon2_text = document.getElementById("weapon2_input");
        
        var weapons = new Array();
        if(weapon1_text.value != "")
                weapons.push(weapon1_text.value);
        if(weapon2_text.value != "")
                weapons.push(weapon2_text.value);
        
        alert(checkSoldierConstraints(allegiance_text.value, armored_text.value, weapons));

        
        
        
}

function deploySquad(){
        
        
        var squad_table = document.getElementById("squad_table");
        
        
        while(soldiers.length){
        
        soldier = soldiers.pop();

                var td1 = document.createElement("td");
                var td2 = document.createElement("td");
                var td3 = document.createElement("td");
        
                td1.innerHTML = soldier.allegiance;
                td2.innerHTML = soldier.armored;
                
        
                if(soldier.weapons.length > 1)
                        td3.innerHTML = soldier.weapons[0] + ", " + soldier.weapons[1];
                else
                        td3.innerHTML = soldier.weapons[0];
                
                
                var row = document.createElement("tr");
                row.appendChild(td1);
                row.appendChild(td2);
                row.appendChild(td3);
                squad_table.appendChild(row);
        } 
        
} 

        

window.onload = init;
<!doctype HTML>
<html>
<head>

<title> Brandon Bell </title>

<script src = "objects_part2.js"> </script>

<style>
                        table, th, td {
                                border: 1px solid black;
                                border-collapse: collapse;
                                text-align: center;
                        }
                        
</style>

</head>

<body>
<h1>Squad Builder</h1>

<input type="text" id="allegiance_input" placeholder=" Allegiance">
                        <input type="text" id="armored_input" placeholder="Armored (True/False)"><br>
                        <input type="text" id="weapon1_input" placeholder="Weapon 1">
                        <input type="text" id="weapon2_input" placeholder="Weapon 2"><br><br>
                        <input type="button" id="add_soldier_button" value="Add Soldier">
                                <input type="button" id="deploy_squad_button" value="Deploy Squad">
</br></br>
                                <table id="squad_table" width=50%>
                        <tr>
                                <th>Allegiance</th>
                                <th>Armored</th>
                                <th>Weapons</th>
                        </tr>
                </table>
</body>


</html>

i am trying to get some if statements to work. particularly the one that if you are tzeenech that you have to use only magic. but even when i put in the allegiance of tzeenech it says that i have to pick the ones that explained in my code. but when i use the ones i said i could use it wont let me. I also dont understand if its because i did .value or not.

Aucun commentaire:

Enregistrer un commentaire