2018-09-30 22:01:58 +00:00
---
id: 596e414344c3b2872167f0fe
2020-11-27 18:02:05 +00:00
title: Comma quibbling
2018-09-30 22:01:58 +00:00
challengeType: 5
2019-08-05 16:17:33 +00:00
forumTopicId: 302234
2021-01-13 02:31:00 +00:00
dashedName: comma-quibbling
2018-09-30 22:01:58 +00:00
---
2020-11-27 18:02:05 +00:00
# --description--
Comma quibbling is a task originally set by Eric Lippert in his [blog ](https://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx ).
# --instructions--
2019-03-01 08:10:50 +00:00
2019-02-25 04:36:09 +00:00
Write a function to generate a string output which is the concatenation of input words from a list/sequence where:
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
< ol >
2019-06-14 11:04:16 +00:00
< li > An input of no words produces the output string of just the two brace characters (< code > "{}"< / code > )< / li >
< li > An input of just one word, e.g. < code > ["ABC"]< / code > , produces the output string of the word inside the two braces, e.g. < code > "{ABC}"< / code > < / li >
< li > An input of two words, e.g. < code > ["ABC", "DEF"]< / code > , produces the output string of the two words inside the two braces with the words separated by the string < code > " and "< / code > , e.g. < code > "{ABC and DEF}"< / code > < / li >
< li > An input of three or more words, e.g. < code > ["ABC", "DEF", "G", "H"]< / code > , produces the output string of all but the last word separated by < code > ", "< / code > with the last word separated by < code > " and "< / code > and all within braces; e.g. < code > "{ABC, DEF, G and H}"< / code > < / li >
2019-02-25 04:36:09 +00:00
< / ol >
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
Test your function with the following series of inputs showing your output here on this page:
2020-11-27 18:02:05 +00:00
2019-02-25 04:36:09 +00:00
< ul >
< li > [] # (No input words).< / li >
< li > ["ABC"]< / li >
< li > ["ABC", "DEF"]< / li >
< li > ["ABC", "DEF", "G", "H"]< / li >
< / ul >
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
**Note:** Assume words are non-empty strings of uppercase characters for this task.
# --hints--
`quibble` should be a function.
```js
assert(typeof quibble === 'function');
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`quibble(["ABC"])` should return a string.
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
assert(typeof quibble(['ABC']) === 'string');
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
`quibble([])` should return "{}".
2018-09-30 22:01:58 +00:00
```js
2020-11-27 18:02:05 +00:00
assert.equal(quibble(testCases[0]), results[0]);
```
2020-09-15 16:57:40 +00:00
2020-11-27 18:02:05 +00:00
`quibble(["ABC"])` should return "{ABC}".
```js
assert.equal(quibble(testCases[1]), results[1]);
```
`quibble(["ABC", "DEF"])` should return "{ABC and DEF}".
```js
assert.equal(quibble(testCases[2]), results[2]);
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
`quibble(["ABC", "DEF", "G", "H"])` should return "{ABC,DEF,G and H}".
```js
assert.equal(quibble(testCases[3]), results[3]);
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --seed--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
## --after-user-code--
2018-09-30 22:01:58 +00:00
```js
2018-10-20 18:02:47 +00:00
const testCases = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]];
const results = ["{}", "{ABC}", "{ABC and DEF}", "{ABC,DEF,G and H}"];
2018-09-30 22:01:58 +00:00
```
2020-11-27 18:02:05 +00:00
## --seed-contents--
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
```js
function quibble(words) {
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
return true;
}
```
2018-09-30 22:01:58 +00:00
2020-11-27 18:02:05 +00:00
# --solutions--
2018-09-30 22:01:58 +00:00
```js
2019-02-26 08:07:07 +00:00
function quibble(words) {
2018-09-30 22:01:58 +00:00
return "{" +
words.slice(0, words.length - 1).join(",") +
(words.length > 1 ? " and " : "") +
2018-10-20 18:02:47 +00:00
(words[words.length - 1] || '') +
2018-09-30 22:01:58 +00:00
"}";
}
```