2018-09-30 23:01:58 +01:00
---
id: 594810f028c0303b75339ad4
2020-11-27 19:02:05 +01:00
title: Word wrap
2018-09-30 23:01:58 +01:00
challengeType: 5
2019-08-05 09:17:33 -07:00
forumTopicId: 302344
2021-01-13 03:31:00 +01:00
dashedName: word-wrap
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-07-18 17:32:12 +02:00
2019-03-11 16:21:45 +09:00
Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --instructions--
2019-07-18 17:32:12 +02:00
2019-03-11 16:21:45 +09:00
Write a function that can wrap this text to any number of characters. As an example, the text wrapped to 80 characters should look like the following:
2020-11-27 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
< pre >
Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX
algorithm. If your language provides this, you get easy extra credit, but you
must reference documentation indicating that the algorithm is something better
2019-06-14 20:04:16 +09:00
than a simple minimum length algorithm.
2018-09-30 23:01:58 +01:00
< / pre >
2020-11-27 19:02:05 +01:00
# --hints--
wrap should be a function.
```js
assert.equal(typeof wrap, 'function');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
wrap should return a string.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert.equal(typeof wrap('abc', 10), 'string');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
wrap(80) should return 4 lines.
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(wrapped80.split('\n').length === 4);
```
Your `wrap` function should return our expected text.
```js
assert.equal(wrapped80.split('\n')[0], firstRow80);
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
wrap(42) should return 7 lines.
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(wrapped42.split('\n').length === 7);
```
Your `wrap` function should return our expected text.
```js
assert.equal(wrapped42.split('\n')[0], firstRow42);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --seed--
## --after-user-code--
2018-09-30 23:01:58 +01:00
```js
2018-10-20 21:02:47 +03:00
const text =
`Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX algorithm.
If your language provides this, you get easy extra credit,
but you ''must reference documentation'' indicating that the algorithm
2020-02-08 13:29:10 -05:00
is something better than a simple minimum length algorithm.`;
2018-10-20 21:02:47 +03:00
const wrapped80 = wrap(text, 80);
const wrapped42 = wrap(text, 42);
const firstRow80 =
'Wrap text using a more sophisticated algorithm such as the Knuth and Plass TeX';
const firstRow42 = 'Wrap text using a more sophisticated';
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function wrap(text, limit) {
return text;
}
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2019-03-11 16:21:45 +09:00
function wrap(text, limit) {
2018-10-20 21:02:47 +03:00
const noNewlines = text.replace('\n', '');
2018-09-30 23:01:58 +01:00
if (noNewlines.length > limit) {
// find the last space within limit
const edge = noNewlines.slice(0, limit).lastIndexOf(' ');
if (edge > 0) {
const line = noNewlines.slice(0, edge);
const remainder = noNewlines.slice(edge + 1);
return line + '\n' + wrap(remainder, limit);
}
}
return text;
}
```