Forum Moderators: open
This is what I want
Lunchbox1 -
Lunchbox2 -
Lunchbox3 -
Now I want to take Lunchbox1, lunchbox2 and lunchbox3 and put it in an array. Then I want to grab there contents and put them into an array. Now the question here is do I need to do them as array1 - for drinks, array2 - for sandwich and array3 - for dessert. Or is there an esier way of doing this?
Thanks
If so, you might get away with one generic "food" array. You need to be sure that the 1modulo3 element is always a beverage, 2modulo3 is always a sandwich, and 0modulo3 is always a sidedish. But that might be prone to errors - you're depending on an ordering convention that might not always be true down the line.
Alternately, if each of the sub-groups have some dependable characteristic, you could define all the elements not worrying about the order -- and then use the array sort() method to group all your beverages in the first third of the array positions, sandwiches in the second third, etc. This is still prone to error, unless you have strict control of the elements.
Long run, it's probably safer and clearer for future maintenance to create separate arrays for each of beverage, sandwich and sidedish. IMHO, of course.
<html>
<head>
</head>
<body>
<script>var cafe = new Array();
function Lunchbox(drink,sandwich,side) {
this.drink = drink;
this.sandwich = sandwich;
this.side = side;
cafe[cafe.length] = this;
}
function createLunchboxes() {
new Lunchbox("White Milk","Bolonie Sandwhich","Bannana");
new Lunchbox("Chocolate Milk","Ham & Cheese Sandwich","Apple");
new Lunchbox("Coca Cola","Hamburger","French Fries");
}
function displayLunchboxes() {
for (var i = 0; i < cafe.length; ++i) {
document.write(
"Lunchbox " + i + ":<br>" +
"Drink: " + cafe[i].drink + "<br>" +
"Sandwich: " + cafe[i].sandwich + "<br>" +
"Side: " + cafe[i].side + "<br><br>"
);
}
}
createLunchboxes();
displayLunchboxes();
</script>
</body>
</html>