2022-01-21 01:00:18 +05:30
---
id: 596e414344c3b2872167f0fe
2022-01-22 20:38:20 +05:30
title: コンマキブリング
2022-01-21 01:00:18 +05:30
challengeType: 5
forumTopicId: 302234
dashedName: comma-quibbling
---
# --description--
2022-01-22 20:38:20 +05:30
カンマキブリング はもともとエリック・リッパートが彼の [ブログ ](https://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx ) で提示したタスクです。
2022-01-21 01:00:18 +05:30
# --instructions--
2022-01-22 20:38:20 +05:30
リスト/シーケンスからの入力単語の連結である文字列を出力する関数を作成します。
2022-01-21 01:00:18 +05:30
< ol >
2022-01-22 20:38:20 +05:30
< li > 単語を入力しなかった場合、2つの中括弧文字 (< code > "{}"< / code > ) だけの出力文字列が生成されます。< / li >
< li > 1単語、例えば < code > ["ABC"]< / code > 、だけを入力した場合、< code > "{ABC}"< / code > のように、2つの中括弧の中にその単語が出力文字列として生成されます。< / li >
< li > 2つの単語、例えば < code > ["ABC", "DEF"]< / code > 、を入力した場合、< code > "{ABC and DEF}"< / code > のように、文字列 < code > " and "< / code > に区切られた2つの単語が、2つの中括弧の中に出力文字列として生成されます。< / li >
< li > 3つ以上の単語、 例えば < code > ["ABC", "DEF", "G", "H"]< / code > を入力した場合、< code > "{ABC, DEF, G and H}"< / code > のように、最後の単語以外のすべての単語は、< code > ", "< / code > で区切られ、最後の単語は < code > " and "< / code > で区切られ、すべての単語が中括弧の中に出力文字列として生成されます。< / li >
2022-01-21 01:00:18 +05:30
< / ol >
2022-01-22 20:38:20 +05:30
次の一連の入力によりこのページに出力表示して、関数をテストします。
2022-01-21 01:00:18 +05:30
< ul >
2022-01-22 20:38:20 +05:30
< li > [] # (入力単語なし)< / li >
2022-01-21 01:00:18 +05:30
< li > ["ABC"]< / li >
< li > ["ABC", "DEF"]< / li >
< li > ["ABC", "DEF", "G", "H"]< / li >
< / ul >
2022-01-22 20:38:20 +05:30
**注記:** このタスクで、単語は大文字の空でない文字列です。
2022-01-21 01:00:18 +05:30
# --hints--
2022-01-22 20:38:20 +05:30
`quibble` という関数です。
2022-01-21 01:00:18 +05:30
```js
assert(typeof quibble === 'function');
```
2022-01-22 20:38:20 +05:30
`quibble(["ABC"])` は文字列を返します。
2022-01-21 01:00:18 +05:30
```js
assert(typeof quibble(['ABC']) === 'string');
```
2022-01-22 20:38:20 +05:30
`quibble([])` は "{}"を返します。
2022-01-21 01:00:18 +05:30
```js
assert.equal(quibble(testCases[0]), results[0]);
```
2022-01-22 20:38:20 +05:30
`quibble(["ABC"])` は "{ABC}"を返します。
2022-01-21 01:00:18 +05:30
```js
assert.equal(quibble(testCases[1]), results[1]);
```
2022-01-22 20:38:20 +05:30
`quibble(["ABC", "DEF"])` は "{ABC and DEF}"を返します。
2022-01-21 01:00:18 +05:30
```js
assert.equal(quibble(testCases[2]), results[2]);
```
2022-01-22 20:38:20 +05:30
`quibble(["ABC", "DEF", "G", "H"])` は "{ABC,DEF,G and H}"を返します。
2022-01-21 01:00:18 +05:30
```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] || '') +
"}";
}
```