dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
153 lines
3.3 KiB
Markdown
153 lines
3.3 KiB
Markdown
---
|
|
id: 56533eb9ac21ba0edf2244ca
|
|
title: Using Objects for Lookups
|
|
challengeType: 1
|
|
forumTopicId: 18373
|
|
dashedName: using-objects-for-lookups
|
|
---
|
|
|
|
# --description--
|
|
|
|
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:
|
|
|
|
```js
|
|
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`.
|
|
|
|
# --instructions--
|
|
|
|
Convert the switch statement into an object called `lookup`. Use it to look up `val` and assign the associated string to the `result` variable.
|
|
|
|
# --hints--
|
|
|
|
`phoneticLookup("alpha")` should equal the string `Adams`
|
|
|
|
```js
|
|
assert(phoneticLookup('alpha') === 'Adams');
|
|
```
|
|
|
|
`phoneticLookup("bravo")` should equal the string `Boston`
|
|
|
|
```js
|
|
assert(phoneticLookup('bravo') === 'Boston');
|
|
```
|
|
|
|
`phoneticLookup("charlie")` should equal the string `Chicago`
|
|
|
|
```js
|
|
assert(phoneticLookup('charlie') === 'Chicago');
|
|
```
|
|
|
|
`phoneticLookup("delta")` should equal the string `Denver`
|
|
|
|
```js
|
|
assert(phoneticLookup('delta') === 'Denver');
|
|
```
|
|
|
|
`phoneticLookup("echo")` should equal the string `Easy`
|
|
|
|
```js
|
|
assert(phoneticLookup('echo') === 'Easy');
|
|
```
|
|
|
|
`phoneticLookup("foxtrot")` should equal the string `Frank`
|
|
|
|
```js
|
|
assert(phoneticLookup('foxtrot') === 'Frank');
|
|
```
|
|
|
|
`phoneticLookup("")` should equal `undefined`
|
|
|
|
```js
|
|
assert(typeof phoneticLookup('') === 'undefined');
|
|
```
|
|
|
|
You should not modify the `return` statement
|
|
|
|
```js
|
|
assert(__helpers.removeJSComments(code).match(/return\sresult;/));
|
|
```
|
|
|
|
You should not use `case`, `switch`, or `if` statements
|
|
|
|
```js
|
|
assert(
|
|
!/case|switch|if/g.test(__helpers.removeJSComments(code).replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g, ''))
|
|
);
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// 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");
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function phoneticLookup(val) {
|
|
let result = "";
|
|
|
|
const lookup = {
|
|
alpha: "Adams",
|
|
bravo: "Boston",
|
|
charlie: "Chicago",
|
|
delta: "Denver",
|
|
echo: "Easy",
|
|
foxtrot: "Frank"
|
|
};
|
|
|
|
result = lookup[val];
|
|
|
|
return result;
|
|
}
|
|
```
|