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) {
// Locate the table body
let tableBody = document.getElementById('menu');
// Remove any existing entries from the table
tableBody.innerHTML = '';
// Add new rows for the items in the list
let length = items.length;
for(let n = 0;n < length;n++) {
let newRow = document.createElement('tr');
newRow.innerHTML = '
'+items[n].item+' | '+items[n].cost+' | ';
tableBody.appendChild(newRow);
}
}
// An example of a filter function designed to filter a list of objects
function filterList(list,meal) {
let result = []; // Make a new, empty list
let length = list.length;
for(let 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) {
let newList = filterList(items,meal);
displayItems(newList);
}
function breakfastHandler() {
displayMeal('B');
}
function lunchHandler() {
displayMeal('L');
}
function dinnerHandler() {
displayMeal('D');
}
function setUp() {
let breakfastButton = document.getElementById('breakfast');
breakfastButton.addEventListener('click',breakfastHandler,false);
let lunchButton = document.getElementById('lunch');
lunchButton.addEventListener('click',lunchHandler,false);
let dinnerButton = document.getElementById('dinner');
dinnerButton.addEventListener('click',dinnerHandler,false);
// Show the lunch menu as the default
displayMeal('L');
}
window.addEventListener('load',setUp,false);