Tell us what’s happening:
I have created the object lookup however the function doesn’t give me the right results when I call it. I don’t know where I went wrong.
**Your code so far**
// 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;
}
phoneticLookup("charlie");
**Your browser information:**
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36
Hi,
I have assigned the result of the lookup object to the variable result. I still get an error when I call the function.
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 result= lookup[val];
// Only change code above this line
return result;
}
phoneticLookup(val);
Ah, JavaScript is getting confused right here. When you are outside of your function, the variable val is not defined. You need to instead call the function like is done in the test cases if you want to test your code:
phoneticLookup("alpha") should equal the string Adams
So the logic I am following is that the function phoneticLookup takes a value (val) and that value is used in the lookup object to access a property. I tried to call the function with two different ways and both of them didn’t work.
I defined the val outside my function before I called the function.
ex: var val=“alpha”
I skipped the defining val and called the function with the value name (ex:phoneticLookup(“alpha”) or phoneticLookup(“charlie”)
When I swap the function call from the tests out for your function call, the code runs correctly. Can you post the updated code that is not working for you?
// 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;
}
var val= "alpha";
phoneticLookup(val);
Make sure you understand the difference between an argument and a parameter.
// callsite, function invocation
phoneticLookup(val);
Here val is an argument. Arguments are values, it must contain a value, or be a value like a literal value (e.g. a string phoneticLookup("someLittralStringValue"))
// function definition
function phoneticLookup(val) {
...come code
}
Here val is a parameter. Parameters are variables capable of holding values pass to it. The value passed is the value of the argument.