Basic JavaScript: Using Objects for Lookups tests bug

Tell us what’s happening:
Hello,

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.

Challenge: Using Objects for Lookups

Link to the challenge:

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.

1 Like

Hi, Do you mean that we can’t use the dot notation with parameters passed into a function?

I tested
var x = val; // “converting” the parameter into a local variable inside the function
lookup.x ; // using dot notation to look into the object

still breaks the test.

dot notation does not evaluate a variable

this is lookup["x"]

dot notation will use what you write exactly to access the object property

2 Likes

You can’t use variables with dot notation.

var x = val;

x is also a variable.

2 Likes

mmmmm :grimacing:

I deffer my fellow coders. :slight_smile:

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.

https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Objects/Basics

My post here is about the test run the lesson has a bug, not if the object’s property can or can’t be accessed via dot notation.

there is no bug in the challenge, lookup.val is undefined as per reasons above

1 Like

Let me be clear. Your code has a bug.

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.

1 Like

Now I see it!!!

:smiley:

It is funny that it accepts a variable but when it is inside brackets.

Many thanks to all.

This is the only way you can access an object property by variable.

2 Likes

the point is that if you use dot notation then it is not a variable, but the exact name

2 Likes

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.

1 Like