Files
.github
api-server
client
config
curriculum
__fixtures__
challenges
_meta
chinese
english
01-responsive-web-design
02-javascript-algorithms-and-data-structures
basic-algorithm-scripting
basic-data-structures
basic-javascript
access-array-data-with-indexes.md
access-multi-dimensional-arrays-with-indexes.md
accessing-nested-arrays.md
accessing-nested-objects.md
accessing-object-properties-with-bracket-notation.md
accessing-object-properties-with-dot-notation.md
accessing-object-properties-with-variables.md
add-new-properties-to-a-javascript-object.md
add-two-numbers-with-javascript.md
adding-a-default-option-in-switch-statements.md
appending-variables-to-strings.md
assigning-the-value-of-one-variable-to-another.md
assignment-with-a-returned-value.md
build-javascript-objects.md
chaining-if-else-statements.md
comment-your-javascript-code.md
comparison-with-the-equality-operator.md
comparison-with-the-greater-than-operator.md
comparison-with-the-greater-than-or-equal-to-operator.md
comparison-with-the-inequality-operator.md
comparison-with-the-less-than-operator.md
comparison-with-the-less-than-or-equal-to-operator.md
comparison-with-the-strict-equality-operator.md
comparison-with-the-strict-inequality-operator.md
comparisons-with-the-logical-and-operator.md
comparisons-with-the-logical-or-operator.md
compound-assignment-with-augmented-addition.md
compound-assignment-with-augmented-division.md
compound-assignment-with-augmented-multiplication.md
compound-assignment-with-augmented-subtraction.md
concatenating-strings-with-plus-operator.md
concatenating-strings-with-the-plus-equals-operator.md
constructing-strings-with-variables.md
count-backwards-with-a-for-loop.md
counting-cards.md
create-decimal-numbers-with-javascript.md
declare-javascript-variables.md
declare-string-variables.md
decrement-a-number-with-javascript.md
delete-properties-from-a-javascript-object.md
divide-one-decimal-by-another-with-javascript.md
divide-one-number-by-another-with-javascript.md
escape-sequences-in-strings.md
escaping-literal-quotes-in-strings.md
find-the-length-of-a-string.md
finding-a-remainder-in-javascript.md
generate-random-fractions-with-javascript.md
generate-random-whole-numbers-with-javascript.md
generate-random-whole-numbers-within-a-range.md
global-scope-and-functions.md
global-vs.-local-scope-in-functions.md
golf-code.md
increment-a-number-with-javascript.md
initializing-variables-with-the-assignment-operator.md
introducing-else-if-statements.md
introducing-else-statements.md
iterate-odd-numbers-with-a-for-loop.md
iterate-through-an-array-with-a-for-loop.md
iterate-with-javascript-do...while-loops.md
iterate-with-javascript-for-loops.md
iterate-with-javascript-while-loops.md
local-scope-and-functions.md
logical-order-in-if-else-statements.md
manipulate-arrays-with-pop.md
manipulate-arrays-with-push.md
manipulate-arrays-with-shift.md
manipulate-arrays-with-unshift.md
manipulating-complex-objects.md
modify-array-data-with-indexes.md
multiple-identical-options-in-switch-statements.md
multiply-two-decimals-with-javascript.md
multiply-two-numbers-with-javascript.md
nest-one-array-within-another-array.md
nesting-for-loops.md
passing-values-to-functions-with-arguments.md
practice-comparing-different-values.md
profile-lookup.md
quoting-strings-with-single-quotes.md
record-collection.md
replace-loops-using-recursion.md
replacing-if-else-chains-with-switch.md
return-a-value-from-a-function-with-return.md
return-early-pattern-for-functions.md
returning-boolean-values-from-functions.md
selecting-from-many-options-with-switch-statements.md
shopping-list.md
stand-in-line.md
store-multiple-values-in-one-variable-using-javascript-arrays.md
storing-values-with-the-assignment-operator.md
subtract-one-number-from-another-with-javascript.md
testing-objects-for-properties.md
understand-string-immutability.md
understanding-boolean-values.md
understanding-case-sensitivity-in-variables.md
understanding-undefined-value-returned-from-a-function.md
understanding-uninitialized-variables.md
updating-object-properties.md
use-bracket-notation-to-find-the-first-character-in-a-string.md
use-bracket-notation-to-find-the-last-character-in-a-string.md
use-bracket-notation-to-find-the-nth-character-in-a-string.md
use-bracket-notation-to-find-the-nth-to-last-character-in-a-string.md
use-conditional-logic-with-if-statements.md
use-multiple-conditional-ternary-operators.md
use-recursion-to-create-a-countdown.md
use-recursion-to-create-a-range-of-numbers.md
use-the-conditional-ternary-operator.md
use-the-parseint-function-with-a-radix.md
use-the-parseint-function.md
using-objects-for-lookups.md
word-blanks.md
write-reusable-javascript-with-functions.md
basic-javascript-rpg-game
debugging
es6
functional-programming
functional-programming-spreadsheet
intermediate-algorithm-scripting
intermediate-javascript-calorie-counter
javascript-algorithms-and-data-structures-projects
object-oriented-programming
regular-expressions
03-front-end-libraries
04-data-visualization
05-apis-and-microservices
06-quality-assurance
07-scientific-computing-with-python
08-data-analysis-with-python
09-information-security
10-coding-interview-prep
11-machine-learning-with-python
12-certificates
dictionaries
schema
test
.babelrc
LICENSE.md
create-challenge-bundle.js
crowdin.yml
getChallenges.acceptance.test.js
getChallenges.js
getChallenges.test.js
gulpfile.js
lib.js
package-entry.js
package-lock.json
package.json
utils.js
cypress
docs
tools
utils
.editorconfig
.eslintignore
.eslintrc.json
.gitattributes
.gitignore
.gitpod.yml
.node-inspectorrc
.npmrc
.prettierignore
.prettierrc
.snyk
.vcmrc
CODE_OF_CONDUCT.md
CONTRIBUTING.md
HoF.md
LICENSE.md
README.md
SECURITY.md
change_volumes_owner.sh
cypress-install.js
cypress.json
jest.config.js
lerna.json
lighthouserc.js
package-lock.json
package.json
sample.env
2020-09-29 22:09:04 +02:00

3.2 KiB

id, title, challengeType, videoUrl, forumTopicId
id title challengeType videoUrl forumTopicId
56533eb9ac21ba0edf2244ca Using Objects for Lookups 1 https://scrimba.com/c/cdBk8sM 18373

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 a simple reverse alphabet lookup:
var alpha = {
  1:"Z",
  2:"Y",
  3:"X",
  4:"W",
  ...
  24:"C",
  25:"B",
  26:"A"
};
alpha[2]; // "Y"
alpha[24]; // "C"

var value = 2;
alpha[value]; // "Y"

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.

Tests

tests:
  - text: <code>phoneticLookup("alpha")</code> should equal <code>"Adams"</code>
    testString: assert(phoneticLookup("alpha") === 'Adams');
  - text: <code>phoneticLookup("bravo")</code> should equal <code>"Boston"</code>
    testString: assert(phoneticLookup("bravo") === 'Boston');
  - text: <code>phoneticLookup("charlie")</code> should equal <code>"Chicago"</code>
    testString: assert(phoneticLookup("charlie") === 'Chicago');
  - text: <code>phoneticLookup("delta")</code> should equal <code>"Denver"</code>
    testString: assert(phoneticLookup("delta") === 'Denver');
  - text: <code>phoneticLookup("echo")</code> should equal <code>"Easy"</code>
    testString: assert(phoneticLookup("echo") === 'Easy');
  - text: <code>phoneticLookup("foxtrot")</code> should equal <code>"Frank"</code>
    testString: assert(phoneticLookup("foxtrot") === 'Frank');
  - text: <code>phoneticLookup("")</code> should equal <code>undefined</code>
    testString: assert(typeof phoneticLookup("") === 'undefined');
  - text: You should not modify the <code>return</code> statement
    testString: assert(code.match(/return\sresult;/));
  - text: You should not use <code>case</code>, <code>switch</code>, or <code>if</code> statements
    testString: assert(!/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g,'')));

Challenge Seed

// Setup
function phoneticLookup(val) {
  var 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");

Solution

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

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

  result = lookup[val];

  return result;
}