--- id: 596e414344c3b2872167f0fe title: コンマキブリング challengeType: 5 forumTopicId: 302234 dashedName: comma-quibbling --- # --description-- カンマキブリング はもともとエリック・リッパートが彼の [ブログ](https://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx) で提示したタスクです。 # --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 " で区切られ、すべての単語が中括弧の中に出力文字列として生成されます。
次の一連の入力によりこのページに出力表示して、関数をテストします。 **注記:** このタスクで、単語は大文字の空でない文字列です。 # --hints-- `quibble` という関数です。 ```js assert(typeof quibble === 'function'); ``` `quibble(["ABC"])` は文字列を返します。 ```js assert(typeof quibble(['ABC']) === 'string'); ``` `quibble([])` は "{}"を返します。 ```js assert.equal(quibble(testCases[0]), results[0]); ``` `quibble(["ABC"])` は "{ABC}"を返します。 ```js assert.equal(quibble(testCases[1]), results[1]); ``` `quibble(["ABC", "DEF"])` は "{ABC and DEF}"を返します。 ```js assert.equal(quibble(testCases[2]), results[2]); ``` `quibble(["ABC", "DEF", "G", "H"])` は "{ABC,DEF,G and H}"を返します。 ```js assert.equal(quibble(testCases[3]), results[3]); ``` # --seed-- ## --after-user-code-- ```js const testCases = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]]; const results = ["{}", "{ABC}", "{ABC and DEF}", "{ABC,DEF,G and H}"]; ``` ## --seed-contents-- ```js function quibble(words) { return true; } ``` # --solutions-- ```js function quibble(words) { return "{" + words.slice(0, words.length - 1).join(",") + (words.length > 1 ? " and " : "") + (words[words.length - 1] || '') + "}"; } ```