Basic JavaScript - Using Objects for Lookups

Tell us what’s happening:
Describe your issue in detail here.

Hi there,
this question is not about the task but about the example below:

while the lines [

const value = “title”;
const valueLookup = article[value];

] make sense as value is a variable that is passed into the following brackets it does not need ’ ’ or " " for my understanding.

But the other part of the code [

const articleAuthor = article[author];
const articleLink = article[link];

] does not work if I try it without the ’ ’ . It only works with the ’ ’ or the dot notation like article.author or article.link.

Sorry for bothering I am quite new to JS and hope someone wise can help with an explanation :slight_smile:

Best regards,
Luk

Your code so far

Using Objects for Lookups

Objects can be thought of as a key/value storage, like a dictionary. If you have tabular data, you can use an object to lookup values rather than a switch statement or an if/else chain. This is most useful when you know that your input data is limited to a certain range.

Here is an example of an article object:

const article = {
  "title": "How to create objects in JavaScript",
  "link": "https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-in-javascript-b0e2450655e8/",
  "author": "Kaashan Hussain",
  "language": "JavaScript",
  "tags": "TECHNOLOGY",
  "createdAt": "NOVEMBER 28, 2018"
};

const articleAuthor = article[author];
const articleLink = article[link];

const value = "title";
const valueLookup = article[value];

articleAuthor is the string Kaashan Hussain, articleLink is the string https://www.freecodecamp.org/news/a-complete-guide-to-creating-objects-in-javascript-b0e2450655e8/, and valueLookup is the string How to create objects in JavaScript.

// Setup
function phoneticLookup(val) {
  let result = "";

  // Only change code below this line
  switch(val) {
    case "alpha":
      result = "Adams";
      break;
    case "bravo":
      result = "Boston";
      break;
    case "charlie":
      result = "Chicago";
      break;
    case "delta":
      result = "Denver";
      break;
    case "echo":
      result = "Easy";
      break;
    case "foxtrot":
      result = "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/107.0.0.0 Safari/537.36

Challenge: Basic JavaScript - Using Objects for Lookups

Link to the challenge:

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

So as someone else said this is a bug. The code in the example is written incorrectly.

Whenever we are accessing a property by its actual name within square brackets it MUST be within quotes, ONLY when it is a variable does it NOT need quotes.

When it does not have quotes JS looks for a variable of this name and therefore throws
us a reference error as such a variable does not exist.

Hi thanks for your answer, just wanted to be sure I got this correctly :slight_smile:

1 Like

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