Using Objects for Lookups - Can you help explain what's going on?

Using Objects for Lookups - Can you help explain what's going on?
0

#1

I ned some help understanding what’s going on here. Does val = var lookup? Since when you call the value “charlie”, it outputs “Chicago” - just having trouble understanding.

function phoneticLookup(val) {
  var result = "";

  var lookup = {
      "alpha": "Adams",
      "bravo": "Boston",
      "charlie": "Chicago",
      "delta": "Denver",
      "echo": "Easy",
      "foxtrot": "Frank"
  };
  return lookup[val];
}

phoneticLookup("charlie");

#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

val contains the argument passed in from the phoneticLookup function. In this example, “charlie” is passed into the variable val.

lookup is a JavaScript object. Objects have property/value pairs. In this example, lookup has a property called “charlie” and the value for the “charlie” property is “Chicago”

There are two ways to get the value of a specific object property. One is using dot notation. If I wanted to get the value of of property “echo” in the lookup object using dot notation, I would write lookup.echo

The other way to get the value of a specific object property is to use bracket notation. There are two times you absolutely must use bracket notation. The first time is if the property name has a space in it. For example, if the lookup object would have had a property called “fox trot” instead of “foxtrot”, then to get the value of “fox trot”, you would have to write lookup[“fox trot”]. The second time you would have to use bracket notation is if you are using a variable for the property name. In your example, since val is a variable which contains the value “charlie”, we have to write lookup[val] to get the “Chicago” value.

Hopefully this clears up any misunderstanding. If not, ask more questions.


#4

I’m still having trouble wrapping my head around what’s going on. Could you explain what each line of code means or is doing? I’ve commented in my code what my understanding is.

function phoneticLookup(val) { // this is a function phoneticLookup which takes one argument, val.
  var result = ""; // not sure what this means... is it printing something?

  var lookup = { // this is an object with properties on the left side and the propertie's values on the right.
      "alpha": "Adams",
      "bravo": "Boston",
      "charlie": "Chicago",
      "delta": "Denver",
      "echo": "Easy",
      "foxtrot": "Frank"
  };
  return lookup[val]; // very confused about what this is doing. i know it's **returning** something but i don't understand what.
}

phoneticLookup("charlie"); // this is calling the function with property 'charlie', which returns its value, chicago.

#5
var result = "";  // declares a variable called result to an empty string.  This is not needed in the solution and can be deleted.

As I explained in my first reply, the following line:

return lookup[val];

returns something which you already understand. Since lookup is an object and val contains the string value “charlie”, we have to use bracket notation when referencing val with respect to the object called lookup.

In your example *phoneticLookup(“charlie”)**:

return lookup[val];

is actually the same thing as:

return lookup["charlie"]; // because val contains the value "charlie"

#6

How does val contain the value ‘charlie’? Is it because val is an argument of the phoneticLookup functon, which the object lookup is a part of (and the property ‘charlie’ being in the object lookup)?

Thanks for explaining; I’m still really new to this and it’s going over my head.


#7

How does val contain the value ‘charlie’? Is it because val is an argument of the phoneticLookup functon

Yes, you seem to understand now.


#8

Not completely, because in the following exercise, “Testing Objects for Properties,” myObj does not contain the function checkObj with argument checkProp, so how can you use checkProp to pass in myObj’s properties and values, like in the “Using Objects for Lookups” exercise?

var myObj = {
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) {
  // Your Code Here
  
  return "Change Me!";
}

// Test your code by modifying these values
checkObj("gift");


if(myObj.hasOwnProperty(checkProp)) { 
  return myObj[checkProp]; 
} else {
  return "Not Found";
  }
}

#9

I think I’ve finally gotten it!

/* this is a global variable, which means it can be used anywhere in the code 
even if not a part of the function */
var myObj = { 
  gift: "pony",
  pet: "kitten",
  bed: "sleigh"
};

function checkObj(checkProp) { 
// declares function checkObj with PARAMETER checkProp
// whatever is PASSED in place of checkProp becomes an ARGUMENT of checkObj
// for example, checkObj("gift"), "gift" is the argument 
// a parameter is just a placeholder for an argument 

// the if/else statement below is a part of the checkObj function
// when you pass an argument in place of checkProp this code runs.

  if(myObj.hasOwnProperty(checkProp)) { 
    return myObj[checkProp]; 
  } else {
    return "Not Found";
  } 
}

checkObj("gift"); 

// so, when you pass "gift" in checkObj, it runs the code inside of the curly brackets: 

function checkObj("gift") { 
if (myObj.hasOwnProperty("gift")) { // checks if myObj has "gift" as a property
  return myObj["gift"]; // returns value of "gift" if found
  } else {
  return "Not Found";
  }
} // this will return "pony" since "gift" is a property of myObj 






#10

What’s happening:
Hi there. Currently working on this task and really don’t understand where I’m going wrong! I’ve named the object ‘lookup’ per the instructions, changed the notation for each value so that it reads like a list and have set up the item ‘result’ so that it returns ‘val’ for whichever value is called.

I’ve also had a look around other solutions out there as well as looking at w3 schools to see if they have another take and I’m flummoxed! Any help would be much appreciated.

My 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"
    };

result = lookup[val];

  // Only change code above this line
  return result;
}

// Change this value to test
phoneticLookup("delta");

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/using-objects-for-lookups


#11

hi there. i think that you missed the = sign
i think that’s the error, everything else is looking good :slight_smile:
it should be
var lookup = { ... }


#12

We liberal arts types like to see some connection between the properties and their values. In the examples given later in this thread, for instance, it’s easy to see that a pony might be a gift, a kitten is a pet, etc.

But is Charlie in Chicago? Is Frank doing the foxtrot? And why are we cheering for Boston? Here’s the connection: The properties in this list (“alpha” through “foxtrot”) are standardized words representing letters A through F of the alphabet. These words are part of the phonetic alphabet (hence the function “phoneticLookup”). Pilots, emergency personnel, and others who must communicate accurately use this alphabet to ensure that they’re not misunderstood because of, say, radio static or the speaker’s accent. An air traffic controller, for instance, might tell a pilot he’s cleared to land on runway A6. But to make sure the pilot hears the instructions correctly, the controller says “alpha 6,” not “A6.” Likewise, a police officer might call in license plate “HRE 6W7” to a dispatcher as “hotel-romeo-echo six-whiskey-seven.”

So the phonetic alphabet is the common denominator in this list. The word “Adams” begins with “A,” “Boston” begins with “B,” and so forth. Hope that explanation helps anyone who had trouble making sense of these items.