www.webdeveloper.com
Results 1 to 2 of 2

Thread: Using else if in a function

  1. #1
    Join Date
    Jun 2014
    Posts
    1

    Using else if in a function

    hi guys

    i am using a else if statement in a function, if i use if and else it will work but if i use if else if else it stops working.
    here is my function:



    Code:
    function myOutzone(){
        var vehicleW,vehicleL,answer6;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        if (vehicleW>=4.5 || vehicleL>=30) {
    	answer6 = "3"
    	} else if (vehicleW>=3.5 && vehicleW<4.5) || (vehicleL>=25 && vehicleL<30){answer6 = "2"
    	}
    else if (vehicleW>=2.5&&vehicleW<3.5)||(vehicleL>=19&&vehicleL<25) 
    	{
    	answer6 = "1"
    	} 
    else {answer6 = "0"
    	}
    	document.getElementById("demo4").innerHTML = "answer is: " + answer6;
    	} here is my webpage complete:
    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
    
    
    <p>1.Rigid Vehicle<br>
    2. Rigid Vehicle plus Pig Trailer *<br>  
    3. Rigid Vehicle plus Dog Trailer - Category A *<br>
    (Overall unladen combination length does not exceed 19 m)<br>
    4. Rigid Vehicle plus Dog Trailer - Category B*<br>
    (Overall unladen combination length exceeds 19 m) <br>
    5. Articulated Vehicle (Prime Mover & Single Semi Trailer) *<br> 
    6. Low Loader Combination (with or without dolly) *<br> </p>
    
    
    <p>
    Vehicle Type: (1-6) <input type="number" id="selTruck" ><br>
    Vehicle Width: <input type="number" id="vehicleW" ><br>
    Vehicle Length: <input type="number" id="vehicleL"><br>
    Vehicle Height: <input type="number" id="vehicleH"><br>
    <br></p>
    
    <button onclick="myClick()">Press Me</button>
    
    <p id="truck"></p>
    <p id="demo"></p>
    <p id="demo1"></p>
    <p id="demo2"></p>
    <p id="demo3"></p>
    <p id="demo4"></p>
    <p> </p>
    <p id="demo"></p>
    <p id="demo"></p>
    
    <script>
    function myClick(){
    	myBeacon();
    	myTrip();
    	myPower();
    	mySpeed();
    	mySigns();
    	myOutzone();
    	
    }
    
    function myTrip() {
        var selTruck,vehicleW,vehicleL,answer;
    	selTruck = document.getElementById("selTruck").value;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        answer = (selTruck == 1 && vehicleL > 12.5) ||(selTruck == 2 && vehicleL > 22.01)||(selTruck == 3 && vehicleL > 19)||(selTruck == 4 && vehicleL > 25)||(selTruck == 5 && vehicleL > 30) || (selTruck == 6 && vehicleL > 30)? "Yes":"No";
        document.getElementById("truck").innerHTML = "Do you need a Single trip permit: " + answer;
    }
    
    function myBeacon() {
        var vehicleW,vehicleL,answer1;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        answer1 = (vehicleW > 3) ||  (vehicleL > 25)? "Yes":"No";
        document.getElementById("demo").innerHTML = "Do you need a Beacon: " + answer1;
    }
    function myPower() {
        var vehicleH,answer2;
        vehicleH = document.getElementById("vehicleH").value;
        answer2 = (vehicleH > 4.3)? "Yes":"No";
        document.getElementById("demo1").innerHTML = "Do you need a Western Power permit: " + answer2;
    }
    
    function mySpeed() {
        var vehicleW,answer3;
        vehicleW = document.getElementById("vehicleW").value;
        answer3 = (vehicleW > 3.5)? "80":"100";
        document.getElementById("demo2").innerHTML = "Travel Speed is: " + answer3;
    }
    
    function mySigns(){
        var vehicleW,vehicleL,answer4;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        answer4 = (vehicleW>2.5 && vehicleL>25 || (vehicleW<=2.5 && vehicleL>22 && vehicleL<=25))? "Yes":"No";
    	answer5 = (vehicleW>2.5 && vehicleL>25) || (vehicleW<=2.5 && vehicleL>22 && vehicleL<=25)? "Yes":"No";
        document.getElementById("demo2").innerHTML = "Front Warning Sign: " + answer4;
    	document.getElementById("demo3").innerHTML = "Rear Warning Sign: " + answer5;
    }
    
    function myOutzone(){
        var vehicleW,vehicleL,answer6;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        if (vehicleW>=4.5 || vehicleL>=30) {
    	answer6 = "3"
    	} else if (vehicleW>=3.5 && vehicleW<4.5) || (vehicleL>=25 && vehicleL<30){answer6 = "2"
    	}
    else if (vehicleW>=2.5&&vehicleW<3.5)||(vehicleL>=19&&vehicleL<25) 
    	{
    	answer6 = "1"
    	} 
    else {answer6 = "0"
    	}
    	document.getElementById("demo4").innerHTML = "answer is: " + answer6;
    	}
    	
    </script>
    
    </body>
    </html>
    i am a beginner learning as i go, any help would be greatly appreciated.
    cheers

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,680
    Code:
    function myOutzone(){
        var vehicleW,vehicleL,answer6;
        vehicleW = document.getElementById("vehicleW").value;
    	vehicleL = document.getElementById("vehicleL").value;
        if (vehicleW>=4.5 || vehicleL>=30) {
    	 answer6 = "3"
    	}
        else if ((vehicleW>=3.5 && vehicleW<4.5) || (vehicleL>=25 && vehicleL<30)){
         answer6 = "2"
    	}
        else if ((vehicleW>=2.5&&vehicleW<3.5)||(vehicleL>=19&&vehicleL<25)){
       	 answer6 = "1"
    	}
        else {
         answer6 = "0"
    	}
    	document.getElementById("demo4").innerHTML = "answer is: " + answer6;
    	}
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles