2018-09-30 22:01:58 +00:00
---
title: Comma quibbling
id: 596e414344c3b2872167f0fe
challengeType: 5
---
## Description
< section id = 'description' >
2019-05-22 14:30:29 +00: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 08:10:50 +00:00
< / section >
## Instructions
< section id = 'instructions' >
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:
< 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 >
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 04:57:59 +00:00
< strong > Note:< / strong > Assume words are non-empty strings of uppercase characters for this task.
2018-09-30 22:01:58 +00:00
< / section >
## Tests
< section id = 'tests' >
```yml
2018-10-04 13:37:37 +00:00
tests:
- text: < code > quibble</ code > is a function.
2018-10-20 18:02:47 +00:00
testString: assert(typeof quibble === 'function', '< code > quibble< / code > is a function.');
- text: < code > quibble(["ABC"])</ code > should return a string.
testString: assert(typeof quibble(["ABC"]) === 'string', '< code > quibble(["ABC"])< / code > should return a string.');
- text: < code > quibble([])</ code > should return "{}".
testString: assert.equal(quibble(testCases[0]), results[0], '< code > quibble([])< / code > should return "{}".');
- text: < code > quibble(["ABC"])</ code > should return "{ABC}".
testString: assert.equal(quibble(testCases[1]), results[1], '< code > quibble(["ABC"])< / code > should return "{ABC}".');
- text: < code > quibble(["ABC", "DEF"])</ code > should return "{ABC and DEF}".
testString: assert.equal(quibble(testCases[2]), results[2], '< code > quibble(["ABC", "DEF"])< / code > should return "{ABC and DEF}".');
- text: < code > quibble(["ABC", "DEF", "G", "H"])</ code > should return "{ABC,DEF,G and H}".
testString: assert.equal(quibble(testCases[3]), results[3], '< code > quibble(["ABC", "DEF", "G", "H"])< / code > should return "{ABC,DEF,G and H}".');
2018-09-30 22:01:58 +00:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
2019-02-26 08:07:07 +00:00
function quibble(words) {
2018-09-30 22:01:58 +00:00
// Good luck!
return true;
}
```
< / div >
### After Test
< div id = 'js-teardown' >
```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
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```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
"}";
}
```
< / section >