Object lookup - var result

Tell us what’s happening:

What is the relationship between var result = “” and var lookup? How does var result know to replace the string value with the properties of var lookup?

To me it looks like there’s two independent variables within the function, I don’t understand how they’re linked. I feel like there’s a gap in my knowledge which hasn’t been explained in the content so far.

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"
  };
return lookup[val];

// 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; rv:82.0) Gecko/20100101 Firefox/82.0.

Challenge: Using Objects for Lookups

Link to the challenge:

There is no relation between var result="" and var lookup{}
But we can relate them like this:
result = lookup[val];
And if a function has two return statements one below the other only the 1’st return statement will execute. Second return statement will never be executed.

So is var result = "" irrelevant in this exercise If the function is just returning lookup[val]? It seems unnecessarily frustrating.

result is just a variable. If you do return lookup[val] its ok you can remove the declaration statement var result="";

if you don’t use result variable we get error You should not modify the return statement Since return statement uses result you must assign the value to result. And the challenge clearly says assign the associated string to the result variable.

I just don’t understand how the function knows how to assign the strings from var lookup to var result.

Check this Accessing Object Properties with Bracket Notation

if you don’t write result = ... then nothing is assigned to result. There is written var result = "" so the value is an empty string and never changed

but the output of the function is determined by the first return statement, and you have return lookup[val], so that’s what the function returns

if there wasn’t that line, then the function would just output an empty string, as there is return result but the value of result is never changed

Yes.

function returns “Chicago” because if val = “charlie” then lookup[val] =“Chicago”

You have two return statements did you notice.

  1. return lookup[val];
  2. return result; // this line will never be executed because function exits on execution of first return statement i.e. return lookup[val]

yeah, that’s the explanation I have written - is there something unclear there?

No. And did you complete the challenge?

I did but not without looking at the solution. The steps in logic are just unclear to me.

Let me explain:

// Setup

function phoneticLookup(val) {

  var result = ""; // Empty string is assigned to result. So result is "".

  // Only change code below this line

  var lookup={

     "alpha": "Adams",

      "bravo": "Boston",

      "charlie": "Chicago",

      "delta": "Denver",

      "echo": "Easy",

      "foxtrot": "Frank"

  } // we are creating object called lookup here.

 result=lookup[val]; // Accessing the object propertie using bracket notation and variable.

  // Only change code above this line

  return result; // We are returning the result i.e. "Chicago".

}

phoneticLookup("charlie");

We can access the properties of a Javascript object in three ways:

  1. Accessing Object Properties with Variables.
  2. Accessing Object Properties with Bracket Notation. Don’t get confused with accessing array elements with [].
  3. Accessing Object Properties with Dot Notation.

Yes, I have. I was trying to help too

1 Like