Using Objects for Lookups - more elegant?

I just finished the “Using Objects for Lookups” challenge and the tests say that my answer is right, but…
Somehow it doesn’t FEEL right.
Here is the challenge:

Warning: SPOILERS.

**
scroll
**
scroll
**
scroll
**

So my solution was this:

// 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("alpha");

I don’t like the “result = lookup[val]” line that I made, and I don’t like that result is declared in the top of the function and then gets changed later.

Wouldn’t it be much better if we’d just declare:
var result = lookup[val];
and thus leave out the first declaration??
I’m hoping some people here have better suggestions on a more elegant solution or an explanation on why FCC does it this way.
Something a beginner can understand
:slight_smile:

4 Likes

The prewritten code in these challenges is meant to scaffold your answer so you’re not left completely in the dark. This sort of thing really doesn’t affect the operation of the code at all, but in the wild, I’d expect to see this function written something like this:

function phoneticLookup(val) {

  var lookup = {
  "alpha": "Adams",
  "bravo": "Boston",
  "charlie": "Chicago",
  "delta": "Denver",
  "echo": "Easy",
  "foxtrot": "Frank"
  };
  
  return lookup[val];
}
6 Likes

Yeah, it can be done even shorter - even by beginners such as me :smiley:

1 Like

WOW I wish I had seen this when I was struggling with this problem.
The prewritten code, as you say, meant to scaffold the answer, did nothing but confuse and terrify me that I will never understand this stuff. I spent a day and a half going over and over this exercise, searching online for functions and lookups, and then today I was doing yet another review of a handful of topics and I found THIS.

NOW it is crystal clear. I honestly think if I had seen THIS as the example in the first place, it would have made so much more sense!

2 Likes

He was giving me a headache this challenge, you can also simplify some more. :grin:
Here is the code.

// Setup
function phoneticLookup(val) {
    
  // Only change code below this line
   var lookup = {
     "alpha": "Adams",
     
    "bravo": "Boston",
     
    "charlie": "Chicago",
     
    "delta": "Denver",
     
    "echo":"Easy",
     
    "foxtrot": "Frank"
  };
  
  // Only change code above this line
  return  lookup[val];
}

// Change this value to test
phoneticLookup("foxtrot");
1 Like

Thanks man, the “Only change code above this line” was messing with my head when in fact I was having the right mindset here!

Spoiler Alert:

You have to assign the lookup[val] to the result variable.
You should not touch the return.



“foxtrot”: “Frank”
};

result = lookup[val];

// Only change code above this line
return lookup[val];

i need help this messing with me cause i have it right but it is not allowing it.
my code:

// 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"
  };

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

// Change this value to test
phoneticLookup("charlie");

At the top of the function, result is an empty string. At the bottom, you return that empty string, you aren’t using the lookup object or the val argument at all

On a related subject, Todd Motto has written a really interesting blog post on how you can use objects to replace switch statements:

https://toddmotto.com/deprecating-the-switch-statement-for-object-literals/

All of this makes sense, save for the last part: " result = lookup[val];"

Why does nestling [val] in “lookup” make it work? I’m having trouble making that connection for some reason. It seems like additional code should be written to link the two parameters before “result = lookup[val]” should work.

It’s not nested, that’s how you specify you want to look at a key in an object, it’s the basic syntax for it: someObject[somekey] is evaluated to whatever the value of somekey in someObject is.

Hi everyone,

I got this working:

// 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"
};

var val;
result = lookup[val];

// Only change code above this line
return result;

}

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

you are doing lookup[undefined]

I m not getting the code. :pensive:

can you please explain ??

What is it that you don’t understand?

Bro why look ups are used ?

and second in while loop, what is the working of var i = 0. Please see the code below.

var ourArray = [];
var i = 0;
while(i < 5) {
ourArray.push(i);
i++;
}

This is pretty off-topic, if you need help with a different challenge you may prefer to create a new thread so that people will come to help you for that, based on thread title

The while loop has a condition that say that it will keep looping till the variable i has a value less than 5, but you need to create the variable first for that to work

Lookups are used to search things in lists