Using Objects For Loojup

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

Challenge: Using Objects for Lookups

Link to the challenge:

Where are you using the lookup object? You seem to have defined it correctly, but you never use the lookup variable.

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.

  1. I defined the val outside my function before I called the function.
    ex: var val=“alpha”

  2. 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);

That code works fine for me.

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.

2 Likes

That’s weird. I keep getting this error TypeError: Cannot read property ‘message’ of undefined.

if this is your complete code it works for me as well. Try another browser, or saving your code elsewhere then resetting the challenge.

1 Like

It worked after I reset the code. Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.