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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    [help] Storing Multi-dimensional arrays in a variable

    I'm not sure why my code is working. I think it maybe because of incorrect storage of multi arrays.

    Can someone please take a look. Please provide advice on how to improve.
    Code:
    <script type="text/javascript">
    
    var route ="Route A - Toronto to Barrie";
    var jon, don;
    
    var businfo=[
    	//[destination/route, pricing, number of available tickets]
    	["Route A - Toronto to Barrie", 10.10, 38],
    	["Route B - Toronto to Peterborough", 12.30, 38],
    	["Route C - Toronto to Montreal", 42.00, 38],
    	["Route D - Toronto to Thunder Bay", 114.30, 38]
    	];//end multi-array businfo
    
    	 
    	function determineRoute() {
    	for(i=0;  i < 5; i++){
    		if (route === businfo[i][0]) {
    		jon = businfo[i];
    		break;
    		}
    	}
    }
    
    function setNumOfTick(ticketsordered) {
    	don = jon;
    	var tickettotal = don.splice(0,1);
    	tickettotal -= ticketsordered;
    	don.splice(0,1,tickettotal);
    	return(2);
    }//end setPrice
    
    alert(setNumOfTick(35));
    </script>
    Last edited by roxxar; 01-21-2012 at 03:26 AM.

  • #2
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    614
    Thanks
    48
    Thanked 64 Times in 64 Posts
    determineRoute() is never called, so jon and don are both undefined when setNumTick is called (also, trying to splice an undefined variable throws an error);
    "Yeah science!"
    Online Science Tools

  • Users who have thanked djh101 for this post:

    roxxar (01-21-2012)

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by djh101 View Post
    determineRoute() is never called, so jon and don are both undefined when setNumTick is called (also, trying to splice an undefined variable throws an error);
    lol thank you wow.. the was stupid of me... forgetting to call the determineRoute();.. thats what i get for making this program at 5am in the night.. thanks.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,020
    Thanks
    75
    Thanked 4,323 Times in 4,289 Posts
    Also, your loop in derterminRoute() goes one element too many. So if the route is not found, you will get a JavaScript error.

    A safer way to write that would be:
    Code:
    function determineRoute() {
    	for( i=0;  i < businfo.length; i++) {
    		if (route === businfo[i][0]) {
    			jon = businfo[i];
    			break;
    		}
    	}
    }
    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
    •