Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    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:

    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
    Regular Coder LearningCoder's Avatar
    Join Date
    Jan 2011
    Location
    The Pleiades
    Posts
    924
    Thanks
    76
    Thanked 29 Times in 29 Posts
    Good morning,

    I'm not sure if it's a typo on your part but you haven't ended this line of code with a semi-colon:

    Code:
    else if (vehicleW>=2.5&&vehicleW<3.5)||(vehicleL>=19&&vehicleL<25) 
        {
        answer6 = "1"
        }
    Kind regards,
    LC

  • Users who have thanked LearningCoder for this post:

    hooky75 (06-25-2014)

  • #3
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I have tried it with and without the semi colon still doesnt seem to make a difference, if i take the myOutzone() function out of the webpage it produces the text i want but with this function it it doesnt produce any text.
    cheers

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,243
    Thanks
    203
    Thanked 2,555 Times in 2,533 Posts
    With if and else there really is no need of else if.
    Additional brackets required in this situation with multiple conditions.
    Use your error console (F12 key) to expose the syntax errors.

    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"}

    answer6 = "1"; // Why are you making these numeric values strings?


    It is recommended that the opening brace { is placed on the same line as the function, if, else, do, while, or for statement and not on the following line. This is because of something known as automatic semi-colon insertion. If you disregard this advice it is looking out for its chance and one day it will rise up and bite you in the undercarriage, and create an error which can be hard to find. Apart from that every Javascript statement should be followed by a semi-colon (;).

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.
    Last edited by Philip M; 06-25-2014 at 08:57 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    hooky75 (06-25-2014)

  • #5
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks heaps i will give this a go tomorrow,
    im guessing i dont need to make them a string to output them,
    i will fix that tomorrow as well, your help is very much appreciated.

  • #6
    New to the CF scene
    Join Date
    Jun 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks heaps Philip M, I have it all working correctly now!!!!


  •  

    Posting Permissions

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