Using Objects for Lookups - more elegant?

Using Objects for Lookups - more elegant?
0

#1

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:


#2

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

#3

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


#4

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!


#5

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");

#6

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


#7

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];