JSON treatment exercise

In this exercise, why can’t I use bracket notation to pass the key ?

json.forEach(function(val) {
    html += "<div class = 'cat'>";
    // Add your code below this line
    html += `<img src='${val[imageLink]}' alt='${val[altText]}'`
  };


Your browser information:

User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0.

Challenge: Render Images from Data Sources

Link to the challenge:

Hello~!

imageLink is the property name (not a variable), so you access it with dot notation.
Here is a quick example of dot notation and bracket notation:

const myObj = {
  name: "nhcarrigan",
  hobby: "coding"
}

//dot notation
console.log(myObj.name) //prints "nhcarrigan" - name is a property on the object. 

//bracket notation with variable
const fun = "hobby"
console.log(myObj[fun]) // prints "coding" - brackets look for the property that matches the value of the variable

//bracket notation without variable
console.log(myObj["name"]) // prints "nhcarrigan" - in this case I've passed the string into the brackets directly, instead of using a variable.

Hope this helps!

Thanks man,
I did understand, i tried after your explication :

html += `<img src='${val["imageLink"]}' alt='${val["altText"]}'` 

Gave ok in the display although it not ok in validation.
But the exercise request to use property. rsrs
Good explication guy.

1 Like

Glad I could help! Happy coding!

1 Like