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.
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.