freeCodeCamp Challenge Guide: Render Images from Data Sources

freeCodeCamp Challenge Guide: Render Images from Data Sources
0.0 0


Obtaining image links from JSON objects has been explained already. However, how do you present that image as an actual image requires a bit of ingenuity, yet it is simple even if you don’t figure it out yourself. You have to generate html using the variable to complete the link like this:

html += "<img src = '" + val.imageLink + "'>";


Can anyone explain why there area double quotes (’" and "’) around the + val.imageLink + and what this means? Just can’t figure it out!



It’s not that there are double quotes around + val.imageLink + … instead, there are double quotes around the strings to either side of + val.imageLink +. Those strings include a single quote '.

Your finished html needs to look like this, for example: <img src=''>. The url needs to be in quotes - that’s why each of the strings includes the single quote '. You have to include the single quote in the strings because it won’t be a part of the image url variable. When combined, the url ends up wrapped in quotes.

What the code snippet does is concatenate three things: a leading string, a variable and the terminating string. You could rewrite it like this:

var imgStart = "<img src = '";
var imgEnd   = "'>"
var imgLink  = "";

html += imgStart + imgLink + imgEnd;


Thank you, I finally understand :slight_smile: