2018-09-30 23:01:58 +01:00
---
title: Comma quibbling
id: 596e414344c3b2872167f0fe
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302234
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
2019-05-22 23:30:29 +09:00
Comma quibbling is a task originally set by Eric Lippert in his <a href="https://blogs.msdn.com/b/ericlippert/archive/2009/04/15/comma-quibbling.aspx" target="_blank">blog</a>.
2019-03-01 17:10:50 +09:00
</section>
## Instructions
<section id='instructions'>
2019-02-25 13:36:09 +09:00
Write a function to generate a string output which is the concatenation of input words from a list/sequence where:
<ol>
2019-06-14 20:04:16 +09: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 13:36:09 +09:00
</ol>
Test your function with the following series of inputs showing your output here on this page:
<ul>
<li>[] # (No input words).</li>
<li>["ABC"]</li>
<li>["ABC", "DEF"]</li>
<li>["ABC", "DEF", "G", "H"]</li>
</ul>
2019-05-23 13:57:59 +09:00
<strong>Note:</strong> Assume words are non-empty strings of uppercase characters for this task.
2018-09-30 23:01:58 +01:00
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
2019-11-20 07:01:31 -08:00
- text: <code>quibble</code> should be a function.
2019-07-26 05:24:52 -07:00
testString: assert(typeof quibble === 'function');
2018-10-20 21:02:47 +03:00
- text: <code>quibble(["ABC"])</code> should return a string.
2019-07-26 05:24:52 -07:00
testString: assert(typeof quibble(["ABC"]) === 'string');
2018-10-20 21:02:47 +03:00
- text: <code>quibble([])</code> should return "{}".
2019-07-26 05:24:52 -07:00
testString: assert.equal(quibble(testCases[0]), results[0]);
2018-10-20 21:02:47 +03:00
- text: <code>quibble(["ABC"])</code> should return "{ABC}".
2019-07-26 05:24:52 -07:00
testString: assert.equal(quibble(testCases[1]), results[1]);
2018-10-20 21:02:47 +03:00
- text: <code>quibble(["ABC", "DEF"])</code> should return "{ABC and DEF}".
2019-07-26 05:24:52 -07:00
testString: assert.equal(quibble(testCases[2]), results[2]);
2018-10-20 21:02:47 +03:00
- text: <code>quibble(["ABC", "DEF", "G", "H"])</code> should return "{ABC,DEF,G and H}".
2019-07-26 05:24:52 -07:00
testString: assert.equal(quibble(testCases[3]), results[3]);
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
2019-02-26 17:07:07 +09:00
function quibble(words) {
2020-09-15 09:57:40 -07:00
2018-09-30 23:01:58 +01:00
return true;
}
```
</div>
### After Test
<div id='js-teardown'>
```js
2018-10-20 21:02:47 +03:00
const testCases = [[], ["ABC"], ["ABC", "DEF"], ["ABC", "DEF", "G", "H"]];
const results = ["{}", "{ABC}", "{ABC and DEF}", "{ABC,DEF,G and H}"];
2018-09-30 23:01:58 +01:00
```
</div>
</section>
## Solution
<section id='solution'>
```js
2019-02-26 17:07:07 +09:00
function quibble(words) {
2018-09-30 23:01:58 +01:00
return "{" +
words.slice(0, words.length - 1).join(",") +
(words.length > 1 ? " and " : "") +
2018-10-20 21:02:47 +03:00
(words[words.length - 1] || '') +
2018-09-30 23:01:58 +01:00
"}";
}
```
</section>