Files
2022-01-23 00:08:20 +09:00

2.9 KiB

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
596e414344c3b2872167f0fe コンマキブリング 5 302234 comma-quibbling

--description--

カンマキブリング はもともとエリック・リッパートが彼の ブログ で提示したタスクです。

--instructions--

リスト/シーケンスからの入力単語の連結である文字列を出力する関数を作成します。

  1. 単語を入力しなかった場合、2つの中括弧文字 ("{}") だけの出力文字列が生成されます。
  2. 1単語、例えば ["ABC"]、だけを入力した場合、"{ABC}" のように、2つの中括弧の中にその単語が出力文字列として生成されます。
  3. 2つの単語、例えば ["ABC", "DEF"]、を入力した場合、"{ABC and DEF}" のように、文字列 " and " に区切られた2つの単語が、2つの中括弧の中に出力文字列として生成されます。
  4. 3つ以上の単語、 例えば ["ABC", "DEF", "G", "H"] を入力した場合、"{ABC, DEF, G and H}" のように、最後の単語以外のすべての単語は、", " で区切られ、最後の単語は " and " で区切られ、すべての単語が中括弧の中に出力文字列として生成されます。

次の一連の入力によりこのページに出力表示して、関数をテストします。

  • [] # (入力単語なし)
  • ["ABC"]
  • ["ABC", "DEF"]
  • ["ABC", "DEF", "G", "H"]

注記: このタスクで、単語は大文字の空でない文字列です。

--hints--

quibble という関数です。

assert(typeof quibble === 'function');

quibble(["ABC"]) は文字列を返します。

assert(typeof quibble(['ABC']) === 'string');

quibble([]) は "{}"を返します。

assert.equal(quibble(testCases[0]), results[0]);

quibble(["ABC"]) は "{ABC}"を返します。

assert.equal(quibble(testCases[1]), results[1]);

quibble(["ABC", "DEF"]) は "{ABC and DEF}"を返します。

assert.equal(quibble(testCases[2]), results[2]);

quibble(["ABC", "DEF", "G", "H"]) は "{ABC,DEF,G and H}"を返します。

assert.equal(quibble(testCases[3]), results[3]);

--seed--

--after-user-code--

const testCases = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]];
const results = ["{}", "{ABC}", "{ABC and DEF}", "{ABC,DEF,G and H}"];

--seed-contents--

function quibble(words) {

  return true;
}

--solutions--

function quibble(words) {
  return "{" +
    words.slice(0, words.length - 1).join(",") +
   (words.length > 1 ? " and " : "") +
   (words[words.length - 1] || '') +
  "}";
}