Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSean Paulson
6,669 PointsMy solution using pugjs
So I'm sure I'm ignorant but his solution seems a bit over engineered. Anyway check out line 12 of cards.js thats the main difference. let me know what you think. Would this break easy if a client told me to change something?
card.js
const express = require('express');
const router = express.Router();
const { data } = require('../data/flashcardData.json');
const { cards } = data;
router.get('/:id', (req, res) => {
const { side } = req.query;
const { id } = req.params;
const text = cards[id][side];
const { hint } = cards[id];
//sets showside to the opposite of side.
let showside = side === "question" ? "answer" : "question";
const templateData = { text, hint, side, id , showside};
res.render('card', templateData);
});
module.exports = router;
card.pug
extends layout.pug
block content
section#content
h2= text
if side == "question"
p
i Hint: #{hint}
a(href=`http://localhost:3000/cards/${id}?side=${showside}`) #{side}
2 Answers
Ben McMahan
7,922 PointsSimilar to what I came up with. One thing that I thought about was that we're relying on the URL being exact with no failsafe for anything other than "question" or "answer". That's where a flushed out else
to handle that would be handy.
Otherwise, I prefer shorthand here.
router.get('/:id', (req, res) => {
// Look for the query string to see which side to display
const {side} = req.query;
// Get the ID of the card
const {id} = req.params;
// Get the text of the selected card
const text = cards[id][side];
// Create text for the flip card link
const sideToShow = side === 'question' ? 'answer' : 'question';
const sideToShowDisplay = side === 'question' ? 'Show Answer' : 'Show Question';
// Get the hint of the selected card
const {hint} = cards[id];
// Create an object to pass into the template
const templateData = { id, text, sideToShow, sideToShowDisplay };
if (side === 'question') {
templateData.hint = hint;
}
res.render('card', templateData) ;
});
Sean Paulson
6,669 PointsI agree very fickle code solution looking back at this now. it gets the point of the lesson across though.