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
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:
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!
He was giving me a headache this challenge, you can also simplify some more.
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");
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
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.
// 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”);
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