Instructions
Access the myStorage object and assign the contents of the glove box property to the gloveBoxContents variable. Use bracket notation for properties with a space in their name.
I don’t know if it’s because I don’t understand english ? I tried doing this
// Setup
var myStorage = {
"car": {
"inside": {
"glove box": "maps",
"passenger seat": "crumbs"
},
"outside": {
"trunk": "jack"
}
}
};
// Only change code below this line
myStorage.car.inside["glove box"];
var gloveBoxContents = ""; // Change this line
But I am still stuck. Can somebody help me please ? Plus, when I click on the get hints I can’t find anything…
I actually feel stupid right now. -_- I kept doing that but got stuck with an error saying dot notation something like that. But now it’s all okay ! Thank you!
Question:
This code doesn’t work because there is a space in glove box correct that is why you use the brackets?
var myStorage = {
“car”: {
“inside”: {
“glove box”: “maps”,
“passenger seat”: “crumbs”
},
“outside”: {
“trunk”: “jack”
}
}
};
// Only change code below this line
var gloveBoxContents = myStorage.car[“inside”].glove box;
That makes perfect sense that a string with a space must use bracket notation, but to be devil’s advocate wouldn’t it be fine to use var gloveBoxContents = myStorage.car[“inside”]["glove box"]; or even var gloveBoxContents = myStorage["car"].inside["glove box"];? Both console.log(gloveBoxContents) to “maps” in Chrome console, but they don’t pass the FCC test. Really confusing what best practices are for dot notation versus bracket (except when there’s a string space or you’re trying to access a variable which both require bracket if I understand correctly).
It may also be that the tests are checking what is written so there are limited ways for solution (as someone could just try to pass the tests assigning directly "maps" to that variable)
Thx much for the response. I’m not sure why my quotes didn’t format correctly in the forum (they were correct in FCC and Chrome console), and I completely understand FCC not anticipating every way a solution could work. I guess my point is that an explanation on when to use dot notation vs. bracket would be helpful.
That’s awesome, and like I said, it was more of a devil’s advocate thing to ensure that they were indeed interchangeable if the string doesn’t have a space. I may be the only camper a bit confused by the ‘rules/best practices’ of dot vs. bracket.
you should always use dot notation unless you can not. It just makes the code more readable
That line was exactly the knowledge I was looking for. Thank you!