...

View Full Version : [help] Storing Multi-dimensional arrays in a variable



roxxar
01-21-2012, 03:18 AM
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.


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

djh101
01-21-2012, 03:36 AM
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);

roxxar
01-21-2012, 03:40 AM
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.

Old Pedant
01-21-2012, 05:25 AM
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:


function determineRoute() {
for( i=0; i < businfo.length; i++) {
if (route === businfo[i][0]) {
jon = businfo[i];
break;
}
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum