// main.js // Run the init() function when the page has loaded window.addEventListener('DOMContentLoaded', init); // Starts the program, all function calls trace back here function init() { // Get the recipes from localStorage let recipes = getRecipesFromStorage(); // Add each recipe to the
element addRecipesToDocument(recipes); // Add the event listeners to the form elements initFormHandler(); } /** * Reads 'recipes' from localStorage and returns an array of * all of the recipes found (parsed, not in string form). If * nothing is found in localStorage for 'recipes', an empty array * is returned. * @returns {Array} An array of recipes found in localStorage */ function getRecipesFromStorage() { // A9. TODO - Complete the functionality as described in this function // header. It is possible in only a single line, but should // be no more than a few lines. let result = JSON.parse(localStorage.getItem('recipes')) if (result) { return result; } return new Array(0); } /** * Takes in an array of recipes and for each recipe creates a * new element, adds the recipe data to that card * using element.data = {...}, and then appends that new recipe * to
* @param {Array} recipes An array of recipes */ function addRecipesToDocument(recipes) { // A10. TODO - Get a reference to the
element let mainEl = document.querySelector('main') recipes.forEach(recipe=> { let newRecipe = document.createElement('recipe-card') newRecipe.data = recipe mainEl.append(newRecipe); }) // A11. TODO - Loop through each of the recipes in the passed in array, // create a element for each one, and populate // each with that recipe data using element.data = ... // Append each element to
} /** * Takes in an array of recipes, converts it to a string, and then * saves that string to 'recipes' in localStorage * @param {Array} recipes An array of recipes */ function saveRecipesToStorage(recipes) { // EXPLORE - START (All explore numbers start with B) // B1. TODO - Complete the functionality as described in this function // header. It is possible in only a single line, but should // be no more than a few lines. localStorage.setItem('recipes', JSON.stringify(recipes)); } /** * Adds the necesarry event handlers to
and the clear storage *