I’m trying to write a piece of code that will run output the results of a function “recipeGen()” multiple times for each member of an object.
The code I have written can output the name of each member of the object I want to effect if told to log to console, but will only run the desired function once before giving me an “uncaught typeError”
How can I change this so that my code outputs “recipeGen()” for every sub member of “recipes”?
Thanks in advance for any help.
p.s. the recipes object I am using is larger than the one posted here but in exactly the same format all the way through. In current state my code will output recipeGen() for the first member “roastRosemaryPotatoes” and give the error when attempting to run the function for the next object.
p.p.s I have also tried using 2 x for/in loops with the same result, hence the Object.keys usage in the second half of function
function outputAllRecipes() {
for (type in recipes) {
console.log(type)
let recipeArray = Object.keys(recipes[type])
console.log(recipeArray)
for (let i = 0; i < recipeArray.length; i++) {
recipeGen(recipes[type][recipeArray[i]])
}
}
}
function recipeGen(recipe) {
let output = `<div class="recipe-box">`
output += `<h3>${recipe.name}</h3>`
output += `<div class="ingredient-list"><h4>Ingredients:</h4>`
output += listIngredArray(recipe) + `</div>`
output += `<h4>Method:</h4>`
output += recipe.method + `</div>`
console.log(output)
return output
}
let recipes = {
side: {
roastRosemaryPotatoes: {
name: "Roast Rosemary Potatoes",
serves: 2,
ingredients: [
['potato', 3],
['rosemary', 1],
['oliveOil', 30],
['saltPepper', 0]
],
method: `<ol>
<li> Preheat oven to 180 degrees</li>
<li> Cut potatoes in to bite sized chunks</li>
<li> In a large bowl, toss the potatoes, rosemary, salt and pepper untill evenly coated</li>
<li> Roast at 180 for ~20 minutes, then turn oven up to 220 degrees and roast for a further 15 - 20 minutes</li>
<li> Serve</li>
</ol>`
},
mashedPotAndsweetPotato: {
name: "Mashed Potato and Sweet Potato",
serves: 2,
ingredients: [
['potato', 2],
['sweetPotato', 1],
['butter', 25],
['saltPepper', 2]
],
method: `<ol>
<li> Roughly dice the potato and steam in a steamer until tender (15 - 20 minutes)</li>
<li> In a large bowl, add the potatoes, butter and salt and pepper to the potatoes and mash with a potato masher in to a puree.
(if you do not have a potato masher, you can use a fork, or a wooden spoon but the result will be quite lumpy. I like potatoes this way but YMMV)</li>
<li> Serve</li>
</ol>`
}
}
}