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 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    55
    Thanks
    26
    Thanked 0 Times in 0 Posts

    Help with declaring variables and using them

    Hi all.

    Have been coding for a while before I found out that I was supposed to be adding 'var' in front of variable declarations. Now that I know this I need to understand something: when I 'change' that variable inside of the function, do I add 'var' in front of the variable name 'there' as well?

    example (see comments):

    Code:
    <script>
    
    //variables declared with var
    
    var car_1_owner;
    	var car_1_mortgage;
    	var upgrade_car_1_1;
    	var upgrade_car_1_2;
    	var upgrade_car_1_3;
    	var car_1_upgrades;
    
          if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 0)
    
    	car_1_owner = "Unowned"; // should I be using 'var car_1_owner' here instead of just 'car_1_owner' ???
    
    	else if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 1)
    	car_1_owner = "Player 1";
    	else if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 2)
    	car_1_owner = "Player 2";
    	else if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 3)
    	car_1_owner = "Player 3";
    	else if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 4)
    	car_1_owner = "Player 4";
    	else if (document.transportation_network_CAR.classic_auto_rentals_1.selectedIndex  == 5)
    	car_1_owner = "Player 5";
    	else
    	car_1_owner = "Player 6";
    					
    	if (document.transportation_network_CAR.classic_auto_rentals_1_d.checked == true)
    	car_1_mortgage = "M";
    	else
    	car_1_mortgage = "-";
    						
    	if (document.transportation_network_CAR.classic_auto_rentals_1_a.checked == true)
    	upgrade_car_1_1 = 1;
    	else
    	upgrade_car_1_1 = 0;
    						
    	if (document.transportation_network_CAR.classic_auto_rentals_1_b.checked == true)
    	upgrade_car_1_2 = 1;
    	else
    	upgrade_car_1_2 = 0;
    						
    	if (document.transportation_network_CAR.classic_auto_rentals_1_c.checked == true)
    	upgrade_car_1_3 = 1;
    	else
    	upgrade_car_1_3 = 0;
    					
    						
    	car_1_upgrades = (upgrade_car_1_1 + upgrade_car_1_2 + upgrade_car_1_3)
    					
    	if (car_1_upgrades == 0)
    	car_1_upgrades = "0";
    </script>
    Sorry, I have searched a bit but am having troubles finding out.

    So... does the 'var' get added on every instance of that element, or JUST on the initial element declaration.

    I hope you all can understand what I'm saying Thanks guys.

  • #2
    Regular Coder
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    578
    Thanks
    4
    Thanked 77 Times in 77 Posts
    No, the var keyword is used upon declaring the variable, not when assigning a value to it, i.e. you only use it once, when you introduce the variable.

  • Users who have thanked Airblader for this post:

    Mindphaser (03-24-2013)

  • #3
    New Coder
    Join Date
    Oct 2012
    Posts
    55
    Thanks
    26
    Thanked 0 Times in 0 Posts
    Thanks Airblader, that will save me a lot of fixing

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,733
    Thanks
    80
    Thanked 4,516 Times in 4,480 Posts
    But you could sure rewrite that code to be TONS smaller and easier to read and maintain!
    Code:
          var form = document.transportation_network_CAR;
          var selix = form.classic_auto_rentals_1.selectedIndex;
          var car_1_owner = ( selix == 0 ) ? "Unowned" : "Player " + selix;
    
          var car_1_mortgage = form.classic_auto_rentals_1_d.checked ? "M" : "-";
          var upgrade_car_1_1 = form.classic_auto_rentals_1_a.checked ? 1 : 0;
          var upgrade_car_1_2 = form.classic_auto_rentals_1_b.checked ? 1 : 0;
          var upgrade_car_1_3 = form.classic_auto_rentals_1_c.checked ? 1 : 0;
          var car_1_upgrades = (upgrade_car_1_1 + upgrade_car_1_2 + upgrade_car_1_3);
    But if you are then going to do this for (say) cars 2, 3, and 4 as well as car 1, then your entire variable naming scheme is broken, as it will just mean you have to keep rewriting the same code. You should be using arrays and probably even arrays of objects.
    Last edited by Old Pedant; 03-24-2013 at 10:16 PM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,733
    Thanks
    80
    Thanked 4,516 Times in 4,480 Posts
    Some "just forr example" code:
    Code:
    function CarInfo( own, mort, up1, up2, up3 )
    {
        this.owner = own;
        this.mortgage = mort;
        this.upgrade_1 = up1;
        this.upgrade_2 = up2;
        this.upgrade_3 = up3;
        this.upgrade = up1 + up2 + up3;
    }
    
    var cars = [ ];
    
    var form = document.transportation_network_CAR;
    for ( var car = 1; car <= 10; ++car ))
    {
        var selix = form["classic_auto_rentals_" + car].selectedIndex;
        var owner = ( selix == 0 ) ? "Unowned" : "Player " + selix;
    
        var mortgage = form["classic_auto_rentals_" + car + "_d"].checked ? "M" : "-";
        var upgrade1 = form["classic_auto_rentals_" + car + "_a"].checked ? 1 : 0;
        var upgrade2 = form["classic_auto_rentals_" + car + "_b"].checked ? 1 : 0;
        var upgrade3 = form["classic_auto_rentals_" + car + "_c"].checked ? 1 : 0;
    
        cars[car] = new CarInfo(owner,mortgage,upgrade1,upgrade2,upgrade3);
    }
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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