function phoneticLookup(val) {
var result = "";
var lookup = {
"alpha": "Adams",
"bravo": "Boston",
"charlie": "Chicago",
"delta": "Denver",
"echo": "Easy",
"foxtrot": "Frank"
};
// After converting our case statements into object properties you can make use of the variable `result` to let the function return the correct value.
result = lookup[val];
// Only change code above this line
return result;
}
Hi everyone. I have a huge problem with this challenege. I got stuck for several hours. Whta do I do wrong?
// Setup
function phoneticLookup(val) {
var result = “”;
// Only change code below this line
var val = {
“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”);
update : use result = lookup[val] instead, that works better without error like @prlndgrn said it.
i was using the original solution with error even when the second return shows unrechable and it even pases the test.
thanks @prlndgrn for the correct info.
// Setup
function phoneticLookup(val) {
var result = “”;
// Only change code below this line
switch(val) {
case “alpha”:
result = “Adams”;
break;
case “bravo”:
result = “Boston”;
break;
case “charlie”:
result = “Chicago”;
break;
case “delta”:
result = “Denver”;
break;
case “echo”:
result = “Easy”;
break;
case “foxtrot”:
result = “Frank”;
}
// Only change code above this line<-------------------------this line should not be here. return result;<---------------------------------------- This is the return that should’ve been changed to: return result = lookup[val];
}
As you can see, it says only to change the code above the line.
This is extremely confusing.
is it a bug? or there’s another way?
// 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");
Yep you got it right, the reason for var result = " " is to create an empty string. This is where the return result; statement actually “returns” the results. It’s like taking the returned results and placing them in a container (box, etc.) You get the results after the code runs but you need someplace to put it so the return statement takes those results and “returns” them into that “box” (which in this case would be var result = " ") Hope that makes sense.
The question I have is when you type result = lookup[val] why do you put val in square brackets? Why not put val in parenthesis like a function argument? What purpose do the square brackets have in this context?
Hey guys,
I tried the bracket notation and it worked, but I tried the dot notation and it failed.
Why can’t I use the dot notation in here, what are the differences between the dot notation and the bracket notation?
Below is my code and it didn’t run well, and please help me, thank you so much!
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”);
// 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");
Brief explanation:
Basically, there’s 3 ways for you to make a sort of look-up dictionary file where you give an input, and get a specific output out
IF / ELSE IF statements
SWITCH / CASE statements
Object array
Specifically, the object’s properties in a key-value pair.
To understand the context of this, I highlighted the keywords here in pseudocode format
OBJECT ARRAY var lookup = {1:value1, 2:value2} → lookup[key]
If key = 1 in all of these examples, you get a return of value1.
Each example here does the same exact thing
Back to the original example:
This is what happens
You calll the function phoneticLookup and pass the value of “charlie”
The function has the object array “Lookup” inside to figure out what to output (“Chicago”)
Inside the function, you store the result by searching through that object array “Lookup” by passing it an input of “Charlie” to output “Chicago”
Return the value
Also, object arrays are VERY common for dictionary lookup files like this. We call this term “JSON” format, or Javascript Object Notation. Used in API calls all the time, transferring information from one app format to another, Grunt.JS files for installing NPM / nodeJS dependencies, etc
Search through a javascript open repo on github and look in the root folder for a .json file and it will follow this example
I tried accessing lookup table using a dot (.) operator like this:
result = lookup.val;
However, this didn’t solved the problem but using bracket notation like below solved it:
result = lookup[val];
I have learned from FCC that both are same. So why didn’t it worked with dot operator? If both work differently, how are they different?
I had trouble with this too, but remember earlier in the Map it said that you must use bracket notation if the property name has a space. The same thing applies if the property name is actually a string.
Think about it this way: lookup.charlie = “Chicago” but lookup.“charlie” will cause an “Identifier not found” error. Dot notation won’t work because the variable val is a actually a string and lookup.val would be interpreted as: lookup.“charlie” instead of lookup.charlie (which is what you actually want). Therefore you must use the bracket notation to pass the string variable into object query in order to access the lookup[“charlie”] value.
Interestingly, if you wanted to change the value of “alpha”, you can just type lookup.alpha = “Alberta”. The compiler knows when your identifier is actually a string, but not when the identifier is actually a string variable.