Lessons 312 to 318 – Complete 2020 Web Development Bootcamp

Lesson 312 – Challenge 13

The goal of this challenge is to write a for loop inside of home.ejs which console logs each post title.

This is the for loop code which completes the challenge –

<% for(let i = 0; i < posts.length; i++) { %>
	<% console.log(posts[i].title); %>
<% }; %>

The new home.ejs code including the added for loop is –

<%- include("partials/header") -%>

<h1>Home</h1>

<p><%= startingContent %></p>

<% for(let i = 0; i < posts.length; i++) { %>
	<% console.log(posts[i].title); %>
<% }; %>

<%- include("partials/footer") -%>

Lesson 314 – Challenge 14

The challenge in this lesson is to refactor the home.ejs file to change the for loop to use the forEach() method instead.

The refactored code is –

<% posts.forEach(function(post) { %>
	<% console.log(post.title); %>
<% }); %>

The home.ejs file with the refactored code is –

<%- include("partials/header") -%>

<h1>Home</h1>

<p><%= startingContent %></p>
 
<% posts.forEach(function(post) { %>
	<% console.log(post.title); %>
<% }); %>

<%- include("partials/footer") -%>

Lesson 315 – Challenge 15

The goal of this challenge is to render the post title and post body on the home page.

This is the refactored code in the forEach function in the home.ejs file –

<% posts.forEach(function(post) { %>
	 <h1> <%= post.title %>  </h1>
	 <p> <%= post.content %> </p>
<% }); %>

The home.ejs file with the refactored code is –

<%- include("partials/header") -%>

<h1>Home</h1>

<p><%= startingContent %></p>
 

<% posts.forEach(function(post) { %>
	 <h1> <%= post.title %>  </h1>
	 <p> <%= post.content %> </p>
<% }); %>


<%- include("partials/footer") -%>

Lesson 318 – Challenge 16

The challenge in this lesson is to change the code so that when you type for example – localhost:3000/posts/Day whatever you type after “posts/” will be printed to the console.

To complete this challenge, you have to create another app.get method in the app.js file. The new app.get method uses Express route parameters to capture and log the name of the parameters.

The code for the new app.get method is –

app.get("/posts/:postName", function(req, res) {
	console.log(req.params.postName);
});