'how to do add two values to same key in and object
I am practising a hackerrank question on Jade template and expressjs. The files are as follows. app.js
// set 'jade' as the 'view engine'
// render the jade template engine with the following data as parameter:
// option:"teachers" / "students",
// Students:["A", "B", "C", "D", "E"],
// Teachers :["AB", "BC", "CD", "DE"]
// run the application on port 8000
const express= require('express');
var app = express();
app.set('views', __dirname + '/views');
app.set("view engine","jade")
app.get('/',(req,res)=>{
res.render('index', {
option:'students' || 'teachers',
Students:["A", "B", "C", "D", "E"],
Teachers :["AB", "BC", "CD", "DE"]
});
})
app.listen(8000)
index.jade
html
title Jade Template Engine
body
h1 Conditions and Loops in Jade
if option==="students"
ol
each name in Students
li #{name}
else if (option === "teachers" )
ol
each name in Teachers
li #{name}
else
p You have entered wrong option!
How do I sent either "students" or "teachers" but not both as parameter to option variable from app.js to index.jade
Solution 1:[1]
This should work...
const express= require('express');
var app = express();
app.set("view engine","jade");
app.set("views","./views/")
app.get('/:option', (req,res) => {
Alpha = ["a", "b", "c", "d", "e"];
Beta = ["f", "g", "h", "i"];
var check = (req.params.option === 'alpha') ? true : (req.params.option === 'beta') ? true : false;
res.render('view', {
options: req.params.option,
hasParams:check,
itemList: (req.params.option === 'alpha') ? Alpha: (req.params.option === 'beta') ? Beta : ''
})
});
app.get('/', (req,res) => {
res.render('view', {hasParams: false})
});
app.listen(8080);
doctype html
html
head
title Page
body
if hasParams
ol
each item in itemList
li #{item}
else
p You have entered wrong option!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Tyler2P |
