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 trialAkshit Jindal
1,341 PointsWhy not using const text=cards[req.params.id].side?
We are using const text=cards[req.params.id][side] in the code. As side is the variable with value as "question" or "answer" and "question" or "answer" are the property of every card, then why const text=cards[req.params.id].side is not working. Thanks in advance.
1 Answer
Zimo Dong
Full Stack JavaScript Techdegree Student 10,755 Pointsside is a variable, in order to access object properties with variables use object[variableName] instead of object.variableName, you can find more info here : https://www.youtube.com/watch?v=aOZUAGucvdo
Seokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsSeokhyun Wie
Full Stack JavaScript Techdegree Graduate 21,606 PointsZimo Dong So it means that
[side]
is not an actual key name, but a variable, so you cannot use with dot notation. Am I right? Guess you're already long gone since you answered on 2017, but if anyone knows I am right or wrong, any answer will be appreciated. Thanks.JASON LEE
17,352 PointsJASON LEE
17,352 PointsSeokhyun Wie ,
cards[id].side =/ cards[id][side]
cards[id].side === cards[id]["side"]
But "side" does not exist as a property of
cards
.if side === "question", then cards[id][side] === cards[id].question
Since
side
is a variable which the value can change, we have to reference using bracket notation.