Challenge - 5 Problems
EJS Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this EJS template render?
Given the following EJS template and data passed from Express, what is the rendered HTML output?
Express
<h1>Welcome, <%= user.name %>!</h1> <ul> <% for(let i = 0; i < user.tasks.length; i++) { %> <li><%= user.tasks[i] %></li> <% } %> </ul>
Attempts:
2 left
💡 Hint
Remember that <%= %> outputs the value, and <% %> runs JavaScript code without output.
✗ Incorrect
The template loops through all tasks in the user.tasks array and outputs each as a list item. The user.name is inserted with <%= %>.
📝 Syntax
intermediate1:30remaining
Identify the syntax error in this EJS snippet
Which option contains the correct syntax to conditionally display a message if
isAdmin is true?Express
<% if (isAdmin) { %>
<p>Welcome, admin!</p>
<% } %>Attempts:
2 left
💡 Hint
Check for proper parentheses and braces in JavaScript if statements inside EJS tags.
✗ Incorrect
Option B correctly uses parentheses and braces for the if statement and closes the EJS tags properly.
❓ state_output
advanced2:00remaining
What is the output of this EJS template with given data?
Consider this EJS template and the data object passed from Express:
Template:
-
<% items.forEach(item => { %>
- <%= item %> <% }) %>
Attempts:
2 left
💡 Hint
The forEach loop outputs each item inside tags.
✗ Incorrect
The template loops through all items and outputs each inside a list item. The output matches the array elements exactly.
🔧 Debug
advanced2:30remaining
Why does this EJS template cause a runtime error?
This EJS template throws an error when rendered:
-
<% for(let i = 0; i < tasks.length; i++) { %>
- <%= tasks[i].name %> <% } %>
Attempts:
2 left
💡 Hint
Check the data type of each item in tasks and what the template expects.
✗ Incorrect
The template tries to access 'name' property on each task, but tasks are strings, not objects, causing a runtime error.
🧠 Conceptual
expert1:30remaining
How does Express find EJS templates by default?
When you set up Express with EJS as the view engine, where does Express look for the template files by default?
Attempts:
2 left
💡 Hint
Express has a default folder name for views unless configured otherwise.
✗ Incorrect
By default, Express looks for templates in a folder named 'views' at the root of the project.