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
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: