I believe there is a bug in the tests ran in this lesson.
the test will fail with this:
result = lookup.val;
but it will pass with this:
result = lookup[val];
Can anyone else confirm this before reporting the bug?
Thanks
Your code so far
// Setup
function phoneticLookup(val) {
var result = "";
// Only change code below this line
var lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
};
result = lookup[val];
// Only change code above this line
return result;
}
phoneticLookup("charlie");
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.146 Safari/537.36.
val is a variable, passed in as the argument to phoneticLookup. You can’t use dot notation with variables. lookup.val translates to lookup["val"] which is not what you want.
Variables that are declared as objects have properties that can be accessed with dot or brackets notation. In my example the varaible lookup is an object.
You cannot access an objects property in this way.
let myDog = {
name: "rover",
legs: 4,
};
let property = "legs";
console.log(myDog.legs); // This is fine
console.log(myDog[property]); // This is also fine
console.log(myDog.property); // This does not work
If you run this code, you will see that the first two return 4 while the third returns undefined.
This is because when you use dot notation, JavaScript is searching for the property that matches the characters following the dot.
Yea, dot notation is for when you want your code to use exactly one property every single time the code is run. Bracket notation is for when your logic changes which property you wish to access.