I am currently attempting to build a site that generates a random meal upon clicking the “generate” button. Attached below will be my code. The issue I am running into is the following; upon clicking the generate button, I am met with numbers, instead of names and nutrition values. I recognize that this is what the Math.random() and Math.floor() are doing - but why is this not converted to an index referencing the food in question? Any help is appreciated. Thank you. For an example of how this should (ideally) work, I will provide a link to a similar project, that has been completed by another user.
<!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>
<p id = "randpick">
Alpha Omega
</p>
</div>
<script src="logic.js"></script>
</body>
</html>
What exactly are you hoping to display when the button is clicked? Are you wanting just the name of the food to show or the other details. If the latter, then you would need to convert the object into a string (one option would be to use the JSON.stringify method.
If you want to format all the details of each object to display them, you will need to decide on how best to display each property’s value. You could iterate through the keys of the object and display the key and the value in separate elements.
Im hoping to display the recipe name as well as the nutrition facts, so essentially all of the properties of each object. Im having a bit of trouble using the
JSON.stringify method, as when I apply it to the foods array, im met with an undeclared variable exception.
I have modified the following code, and now see all of the foods displayed at once. Now, I have to find a way to format these foods, and make it display only one of the foods, not every single object within the array;
function getFood()
{
let string = JSON.stringify(foods);
let randomFood = string[Math.floor(Math.random()*foods.length)];
document.getElementById("randpick").textContent=string;
}
I went back in and re-worked some of my code , and am now able to pull one specific object from the Array. Listed Below will be my updated code;
function getFood()
{
let food = foods.values(foods.name);
let userFood = foods[Math.floor(Math.random()*foods.length)];
let choice = JSON.stringify(userFood)
document.getElementById('randpick').textContent=choice
console.log(userFood);
}
Now, it is displaying ONE food object from the array (which is what I wanted.) How would I go about formatting this? My mind instantly goes to using DOM manipulation, as I don’t believe CSS would work?
You just need to dynamically add new elements by iterating through the objects’s properties (look at Object.keys() or Object.entries() method on how to do something like this.