Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

2.6 KiB
Raw Blame History

id, title, challengeType, videoUrl, forumTopicId, dashedName
id title challengeType videoUrl forumTopicId dashedName
56533eb9ac21ba0edf2244ca 使用对象进行查找 1 https://scrimba.com/c/cdBk8sM 18373 using-objects-for-lookups

--description--

对象和字典一样,可以用来存储键/值对。如果你的数据跟对象一样你可以用对象来查找你想要的值而不是使用switch或if/else语句。当你知道你的输入数据在某个范围时这种查找方式极为有效。

这是简单的反向字母表:

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

把 switch 语句转化为lookup对象。使用它来查找val属性的值,并赋值给result变量。

--hints--

phoneticLookup("alpha")应该等于"Adams"

assert(phoneticLookup('alpha') === 'Adams');

phoneticLookup("bravo")应该等于"Boston"

assert(phoneticLookup('bravo') === 'Boston');

phoneticLookup("charlie")应该等于"Chicago"

assert(phoneticLookup('charlie') === 'Chicago');

phoneticLookup("delta")应该等于"Denver"

assert(phoneticLookup('delta') === 'Denver');

phoneticLookup("echo")应该等于"Easy"

assert(phoneticLookup('echo') === 'Easy');

phoneticLookup("foxtrot")应该等于"Frank"

assert(phoneticLookup('foxtrot') === 'Frank');

phoneticLookup("")应该等于undefined

assert(typeof phoneticLookup('') === 'undefined');

请不要修改return语句。

assert(code.match(/return\sresult;/));

请不要使用caseswitch,或if语句。

assert(
  !/case|switch|if/g.test(code.replace(/([/]{2}.*)|([/][*][^/*]*[*][/])/g, ''))
);

--seed--

--seed-contents--

// 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");

--solutions--

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

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

  result = lookup[val];

  return result;
}