Getting lost with Using Objects for lookups

Getting lost with Using Objects for lookups
0

#1

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.


#2

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.

#3

Thank you!! It worked :slight_smile:


#4

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?


#5

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


#6

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


#7

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?


#8

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

#9

Aah, I see thanks :slight_smile:


#10

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


#11

Yes, it worked. Is this a bug?


#12

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


#13

nice. thanks for the explanation @undeadmonkey .


#14

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


#15

Thanks for the explanation. I got it now.


#16

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]