-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(2023): add solution for challenge #10
- Loading branch information
Showing
4 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Challenge #10: Create your owm Christmas tree | ||
|
||
What an idea Sam Elfman has had! He wants to offer a service that creates a **customized Christmas tree** 🎄 in a matter of seconds. | ||
|
||
To create it, we are given a **string to form the tree** and a **number that indicates its height.** | ||
|
||
Each character of the string represents an ornament of the tree, and we use them **cyclically** until we reach the indicated height. **At least, they will always pass us one.** | ||
|
||
We must return a multiline **string** with the Christmas tree made with the ornaments, the indicated height **plus a final line with the trunk formed by the character `|`** in the center and, finally, a newline `\n`. | ||
|
||
For example, if we receive the string `"123"` and the number `4` as height, we would have to build this tree: | ||
|
||
```js | ||
1 | ||
2 3 | ||
1 2 3 | ||
1 2 3 1 2 | ||
| | ||
``` | ||
|
||
If we receive the string `*@o` and the number `3`, the tree we should return is: | ||
|
||
```js | ||
1 | ||
2 3 | ||
1 2 3 | ||
1 2 3 1 2 | ||
| | ||
``` | ||
|
||
Note: | ||
|
||
- **The tree should always be centered, for that reason add blank spaces to the left of each line.** | ||
- **Create spaces only to the left of each line of the tree. Do not leave blank spaces to the right.** | ||
- **The ornaments have a white space between them for separation.** |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { describe } from 'vitest' | ||
import { createChristmasTree } from './challenge-10' | ||
|
||
const TEST_CASES: TestCases<[string, number], string> = [ | ||
{ args: ['x', 3], expected: ` x\n x x\nx x x\n |\n` }, | ||
{ args: ['xo', 4], expected: ` x\n o x\n o x o\nx o x o\n |\n` }, | ||
{ | ||
args: ['123', 5], | ||
expected: ` 1\n 2 3\n 1 2 3\n 1 2 3 1\n2 3 1 2 3\n |\n`, | ||
}, | ||
{ args: ['*@o', 3], expected: ` *\n @ o\n* @ o\n |\n` }, | ||
] | ||
|
||
describe('Challenge #10: Create your owm Christmas tree', () => { | ||
buildChallengeTestCases({ | ||
cases: TEST_CASES, | ||
spreadFn: createChristmasTree, | ||
}) | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
export function createChristmasTree(ornaments: string, height: number) { | ||
let result = '' | ||
const spaces = ' '.repeat(height - 1) | ||
|
||
const ornamentsArray = [...ornaments.repeat(height)].join(' ') | ||
let currentPosition = 0 | ||
|
||
for (const index of new Array(height).keys()) { | ||
const length = index + 1 | ||
const total = currentPosition + length | ||
|
||
result += | ||
spaces.slice(index) + | ||
ornamentsArray.slice(currentPosition * 2, total * 2 - 1) + | ||
'\n' | ||
|
||
currentPosition = total % ornaments.length | ||
} | ||
|
||
return result + spaces + '|\n' | ||
} |
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