Hello everyone!
This is my second thread dedicated to this project. I am working hard to try and get it to function properly.
I have implemented the random logic - whenever the “generate meal button” is clicked, the Math.Random() + Math.floor() function pulls a random meal from my array, which contains nested objects.
I am having all of the keys and entries popup on the screen each time the user generates a new meal. However, I am having a VERY hard time trying to format this; how would I format the strings that display as a result to not include curly braces, brackets, and be spaced out? Attached below will be all of my JS and HTML.
JS:
// Food(s) array w/nested Objects. //
const foods = [
{ "name": 'Chicken and Rice',
"protein": '45g',
"fat": '12g',
"carbohydrates" : '35g',
"total calories": '428'
},
{ "name":'Guac Omelette',
"protein":'30g',
"fat":'20g',
"carbohydrates":"17g",
"total calories": '368'
},
{
"name":"Beef Rice",
"protein":'48g',
"fat":'22g',
"carbohydrates":'52g',
"total calories":'598'
}
]
for (let i=0; i<foods.length; i++){
console.log(foods[i])
}
function getFood()
{
let userFood = foods[Math.floor(Math.random()*foods.length)];
let food = foods.values(foods.name);
let choice = JSON.stringify(userFood)
document.getElementById('randpick').textContent=choice;
console.log(userFood)
}
HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Random Meal Generator</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1 class="question">Feeling Hungry?</h1>
<h2 class="question">
Struggling to come up with healthy meals?
</h2>
<button class="generate" id="generate" onclick="getFood()">
Click Me To Generate a Random Meal!
</button>
<p class="info">
A balanced macronutrition dietary profile is of utmost importance for overall health and well-being. It refers to the consumption of an appropriate ratio of macronutrients, including carbohydrates, proteins, and fats, in the right proportions. Such a diet provides the body with essential nutrients, energy, and the building blocks necessary for various bodily functions.
</p>
<div id = "randomMeal">
<p id = "randpick">
Alpha Omega
</p>
</div>
</div>
<script src="logic.js"></script>
</body>
</html>