The problem I am having with this exercise is I need to output on my html a list <li></li> and populate it with items from an object which I have attempted several times but cannot get it. As you will see in my html file I already have the <ul></ul> tags with class of "grains". Per the instructions I am trying to create a loop to loop over the object and append each value to the <li> so they can show in my html.
The final output should be:
Here is my index.html:
<!DOCTYPE html>
<html lang="en-ca">
<head>
<meta charset="utf-8">
<title>Grain globber</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="css/main.css" rel="stylesheet">
</head>
<body>
<h1>Grains</h1>
<ul class="grains"></ul>
<script src="jquery.min.js"></script>
<script src="js/grains.js"></script>
<script src="js/main.js"></script>
</body>
</html>
Here is my grains.js which holds my object:
var grains = [
{
name: 'Peanuts',
img: 'peanuts.jpg',
desc: 'First cultivated in the valleys of Paraguay.'
},
{
name: 'Beans',
img: 'beans.jpg',
desc: 'A summer crop that needs warm temperatures.'
},
{
name: 'Lentils',
img: 'lentils.jpg',
desc: 'An edible pulse from a bushy annual plant.'
}
];
Here is my main.js file where I am attempting to complete the task but am stuck on:
$('.grains').each(function(){
document.write($(this).text(grains) + "\n")
});
$("ul").each(function(grains) {
for(var i = 0; i < grains.length;i++){
grains[i].name;
grains[i].img;
grains[i].desc;
if (('body').hasClass('grains')) {
$('ul').append('<li>'+grains[i].name+'</li>'+'<li>'+grains[i].img+'</li>'+'<li>'+grains[i].desc+'</li>');
}
}
});
And this is my output:
I know I can get it done this way (see below) but I need to use a loop:
var $list = $('.grains');
$list.append('<h2>' + grains[0].name + '</h2>');
$list.append('<img src="images/peanuts.jpg">');
$list.append('<p>' + grains[0].desc + '</p>');
$list.append('<h2>' + grains[1].name + '</h2>');
$list.append('<img src="images/beans.jpg">');
$list.append('<p>' + grains[1].desc + '</p>');
$list.append('<h2>' + grains[2].name + '</h2>');
$list.append('<img src="images/lentils.jpg">');
$list.append('<p>' + grains[2].desc + '</p>');
Here are the instructions:
With an already made array of grains, create a layout in the HTML with jQuery & CSS.
- Fork this repository.
- The information is inside an array of objects in the
grains.jsfile.- Loop over the
grainsvariable and use jQuery to output<li>tags into the<ul>that’s already in the HTML file.- Each
<li>tag should have an<img>, an<h2>, & a<p>- Style the Javascript generated list using CSS—the selectors are all ready.
- DO NOT change the HTML.
- DO NOT change
grains.js- Run it through Markbot and make sure it passes all the checks.
Goal
Visually match the images in the “screenshots” folder.
I just need some guidance on getting the loop to work.

