0
0
Expressframework~20 mins

EJS template setup in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
EJS Template Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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>
A<h1>Welcome, Alice!</h1><ul><li>Buy milk</li><li>Walk dog</li></ul>
B<h1>Welcome, user.name!</h1><ul><li>user.tasks[0]</li><li>user.tasks[1]</li><li>user.tasks[2]</li></ul>
C<h1>Welcome, Alice!</h1><ul><li>Buy milk</li><li>Walk dog</li><li>Read book</li></ul>
D<h1>Welcome, Alice!</h1><ul></ul>
Attempts:
2 left
💡 Hint
Remember that <%= %> outputs the value, and <% %> runs JavaScript code without output.
📝 Syntax
intermediate
1: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>
<% } %>
A<% if isAdmin { %><p>Welcome, admin!</p><% } %>
B<% if (isAdmin) { %><p>Welcome, admin!</p><% } %>
C<% if (isAdmin) %><p>Welcome, admin!</p><% } %>
D<% if (isAdmin) { %><p>Welcome, admin!</p>
Attempts:
2 left
💡 Hint
Check for proper parentheses and braces in JavaScript if statements inside EJS tags.
state_output
advanced
2: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 %>
  • <% }) %>
Data: { items: ['apple', 'banana', 'cherry'] } What is the rendered HTML output?
A<ul><li>apple</li><li>banana</li><li>cherry</li></ul>
B<ul><li>items[0]</li><li>items[1]</li><li>items[2]</li></ul>
C<ul><li>apple</li><li>banana</li></ul>
D<ul></ul>
Attempts:
2 left
💡 Hint
The forEach loop outputs each item inside
  • tags.
  • 🔧 Debug
    advanced
    2: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 %>
    • <% } %>
    Data passed: { tasks: ['clean', 'cook', 'shop'] } What causes the error?
    AUsing <%= %> inside a loop is not allowed in EJS
    BSyntax error: missing closing tag for the for loop
    Ctasks is undefined in the template context
    Dtasks array contains strings, but template expects objects with a 'name' property
    Attempts:
    2 left
    💡 Hint
    Check the data type of each item in tasks and what the template expects.
    🧠 Conceptual
    expert
    1: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?
    AIn the 'views' folder located in the root directory of the project
    BIn the 'templates' folder inside the 'public' directory
    CIn the same folder as the server.js file
    DIn the 'ejs' folder inside 'node_modules'
    Attempts:
    2 left
    💡 Hint
    Express has a default folder name for views unless configured otherwise.