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

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 Likes

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.
1 Like

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.

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";
  }
}
1 Like

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 





6 Likes

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

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 = { ... }

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.

This is what I don’t understand. The lesson states not to change the return statement. You can’t move forward with this lesson in any way shape or form whether or not you change the return statement. So how do you successfully pass onto the next lesson then???

Using return lookup[val]; you will receive success on all the tests but also will be told not to change the original return statement. This is very bad teaching.

ugh I got it. not only was I taking out the return result but I was also putting the return lookup[val]; in the wrong part of the code. Thanks!!! :slight_smile:

// 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 lookup[val];
return result;

}

// Change this value to test
phoneticLookup(“delta”);

Try this solution.It worked for me.

You should re-assign the the object lookup to var result with val as argument :

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

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

phoneticLookup("charlie");