var items = [{item:'Felafel sandwich',cost:'$4.50',meal:'L'},
{item:'Cheese pizza',cost:'$4.50',meal:'L'},
{item:'Breakfast Burrito',cost:'$3.50',meal:'B'},
{item:'Hamburger',cost:'$4.00',meal:'L'},
{item:'Mac and Cheese',cost:'$4.50',meal:'D'},
{item:'Muffin',cost:'$2.50',meal:'B'},
{item:'Salisbury Steak',cost:'$6.50',meal:'D'},
{item:'Pancakes',cost:'$3.50',meal:'B'},
{item:'Spaghetti',cost:'$4.50',meal:'D'}];
function displayItems(items) {
var table, newRow;
var n, length;
// Remove any existing entries from the table
$('#menu tr').remove();
// Add new rows for the items in the list
length = items.length;
for(n = 0;n < length;n++) {
newRow = $('
').html(''+items[n].item+' | '+items[n].cost+' | ');
$('#menu').append(newRow);
}
}
// An example of a filter function designed to filter a list of objects
function filterList(list,meal) {
var result, n, length;
result = []; // Make a new, empty list
length = list.length;
for(n = 0;n < length;n++) {
// Search the original list for items that have the desired meal property
if(list[n].meal == meal)
result.push(list[n]); // Add items that match to the new list
}
return result;
}
function displayMeal(meal) {
var newList;
newList = filterList(items,meal);
displayItems(newList);
}
function breakfastHandler() {
displayMeal('B');
}
function lunchHandler() {
displayMeal('L');
}
function dinnerHandler() {
displayMeal('D');
}
function setUp() {
$('button#breakfast').click(breakfastHandler);
$('button#lunch').click(lunchHandler);
$('button#dinner').click(dinnerHandler);
// Show the lunch menu as the default
displayMeal('L');
}
$(document).ready(setUp);