Getting lost with Using Objects for lookups

Here is the code I was doing:
// Setup
function phoneticLookup(val) {
var result = “”;

// Only change code below this line
var phoneticLookup ={
alpha:“Adams”,

bravo: "Boston",
 
charlie: "Chicago",

delta: “Denver”,

echo: "Easy",
  
foxtrot: "Frank",

};

// Only change code above this line
return result;
}

// Change this value to test
phoneticLookup(“charlie”);

When I hit the run test button it keeps saying that
phoneticLookup(“alpha”) should equal "Adams"
phoneticLookup(“bravo”) should equal "Boston"
phoneticLookup(“charlie”) should equal "Chicago"
phoneticLookup(“delta”) should equal "Denver"
phoneticLookup(“echo”) should equal “Easy"
phoneticLookup(“foxtrot”) should equal “Frank"
phoneticLookup(””) should equal undefined
I don’t understand what else I should do? IT all looks equal to me. What is the problem? Any help is appreciated.

You’re doing great so far! Just a few small items to consider:

  • Change the value of the variable result. Currently, result is always returning an empty string.
  • Change the name of your object from phoneticLookup to lookup. This might make it more clear that the tests being run are calling the function phoneticLookup and not the object.
  • The last key:value pair in your object should not have a trailing comma.
7 Likes

Thank you!! It worked :slight_smile:

I tried to follow 0x0936 's considerations… here is what I did:

`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;
}

// Change this value to test
phoneticLookup(“charlie”);`

It still doesn’t work… what am I missing?

nvm. changed. lookup.val to lookup[val] and it worked.

4 Likes

Adding brackets solve my problem too. It seems like it should work with the dot notation and probably would in the real world.

Why does both options work? I don’t understand why you can make your object like this

var lookup = {
alpha: "Adams",
bravo: "Boston",
charlie: "Chicago",
delta: "Denver",
echo: "Easy",
foxtrot: "Frank"
};

And like this:

  var lookup = {
    "alpha": "Adams",
    "bravo": "Boston",
    "charlie": "Chicago",
    "delta": "Denver",
    "echo": "Easy",
    "foxtrot": "Frank"
  };

Is using " " just about style or does it also have another function when you’re making an object?

Enclosing the keys in quotes is simply legal syntax. However if you ever have keys that contain spaces in them only the quoted version will work

var obj = {
  'sample key': 'hello',
  'another one': 'world'
};

// NOT THIS ONE!
var obj2 = {
  sample key: 'hello',
  another one: 'world'
};

Aah, I see thanks :slight_smile:

Thanks, It working.
result = lookup[val];

1 Like

Yes, it worked. Is this a bug?

1 Like

Nope, it’s just syntax. When you’re using a variable for the property it needs to be in brackets.

3 Likes

nice. thanks for the explanation @undeadmonkey .

i still dont get one thing i.e what is var result doing in this function?

1 Like

Thanks for the explanation. I got it now.

dot notation did NOT work in this exercise. Not sure why… What did work is the following:

[spoiler]
// 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;
}

// Change this value to test
phoneticLookup(“charlie”);[/spoiler]

the program is working

You can post solutions that invite discussion (like asking how the solution works, or asking about certain parts of the solution). But please don’t just post your solution for the sake of sharing it.

We have set your post to unlisted. Thanks for your understanding.