Created
June 12, 2024 10:42
-
-
Save julioflima/1f5947299e89aeb98e0affdbdaee0767 to your computer and use it in GitHub Desktop.
Vigil - Backend Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
# backend-challenge | |
After reading the Linux kernel coding style, you discover the magic of having lines of code with a maximum of 80 characters each. | |
So, you decide that from now on your outgoing emails will also follow a similar pattern and you decide to develop a plugin to help you with that. | |
Implement a function that receives: | |
- *any text* | |
- *a character limit per line* | |
and be able to generate a new text broken into lines with a maximum *limit of characters per line* defined. | |
Important note: **No words can be broken**. | |
**Input text example:** | |
"In 1991, while studying computer science at University of Helsinki, Linus Torvalds began a project that later became the Linux kernel. He wrote the program specifically for the hardware he was using and independent of an operating system because he wanted to use the functions of his new PC with an 80386 processor. Development was done on MINIX using the GNU C Compiler." | |
**Example output 40 character:** | |
In 1991, while studying computer science<br> | |
at University of Helsinki, Linus<br> | |
Torvalds began a project that later<br> | |
became the Linux kernel. He wrote the<br> | |
program specifically for the hardware he<br> | |
was using and independent of an<br> | |
operating system because he wanted to<br> | |
use the functions of his new PC with an<br> | |
80386 processor. Development was done on<br> | |
MINIX using the GNU C Compiler.<br> | |
*/ | |
const breakLines = (text, maxLimitCharacters) => { | |
const result = Array(...text).reduce((acc, value, index) => { | |
if ((index - acc.index) === (maxLimitCharacters - 1)) { | |
const splitedEnd = text.slice(acc.index, acc.lastBlank) | |
const joinedText = splitedEnd + '<br>' | |
const result = acc.text + '\n' + joinedText | |
return { ...acc, index: acc.lastBlank + 1, text: result } | |
} | |
if (value === ' ') return { ...acc, lastBlank: index, } | |
return acc | |
}, { index: 0, lastBlank: 0, text: '' }) | |
return result.text | |
} | |
const result = breakLines( | |
"In 1991, while studying computer science at University of Helsinki, Linus Torvalds began a project that later became the Linux kernel. He wrote the program specifically for the hardware he was using and independent of an operating system because he wanted to use the functions of his new PC with an 80386 processor. Development was done on MINIX using the GNU C Compiler.", | |
40 | |
) | |
console.log(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment