AdventJS 2023: Day 10 Challenge
Tags: javascript, webdev, challenge, programming
Published: 2023-12-13 04:40:19

---
title: AdventJS 2023: Day 10 Challenge
published: true
date: 2023-12-10 06:00:00 UTC
tags: javascript, webdev, challenge, programming
canonical_url: https://alexvalle.dev/adventjs-2023-day-10-challenge
cover_image: "https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dkux7g6eatjkx5s21mfs.png"
---

Solution to [challenge #10](https://adventjs.dev/challenges/2023/10) of [AdventJS 2023](https://adventjs.dev)

[Solution to the previous challenge](https://dev.to/fenriuz/adventjs-2023-day-9-challenge-274j)

[Solution to the next challenge](https://dev.to/fenriuz/adventjs-2023-day-11-challenge-3i35)

## Challenge Description

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 new line `\n`.

For example, if we receive the string "123" and the number `4` as height, we would have to build this tree:

```
   1 2 3 1 2 31 2 3 1 |
```

If we receive the string `*@o` and the number `3`, the tree we should return is:

```
  * @ o* @ o |
```

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.

## Analysis

And we're back to drawing! This time it's Christmas trees, which we must draw using the strings passed as parameters and sizing them as indicated.

### Input

1. Ornaments (`ornaments`): A string containing the character sequence that will fill the tree.

2. Height (`height`): The height and maximum width of the tree.

### Output

- A string with the tree's drawing.

### Considerations

- We must add spaces at the beginning to give it the shape of a Christmas tree.

- At the end, we must insert a line break.

## Solution

A simple way to complete the challenge is to calculate the total number of characters required, for which we can use mathematics. Once we have this, we can iterate and fill each row of the tree.

### Code

```javascript
function createChristmasTree(ornaments, height) {
  let tree = "";
  const totalOrnaments = (height * (height + 1)) / 2;
  const allOrnaments = ornaments.repeat(totalOrnaments);
  let lastIndex = 0;
  for (let i = 1; i <= height; i += 1) {
    const spaces = " ".repeat(height - i);
    const ornamentRow = allOrnaments
      .slice(lastIndex, lastIndex + i)
      .split("")
      .join(" ");
    tree += spaces + ornamentRow + "\n";
    lastIndex += i;
  }
  tree += "|".padStart(height, " ") + "\n";
  return tree;
}
```

### Community Solutions

Solution by [iswilljr](https://github.com/iswilljr/adventjs/blob/master/2023/challenge-10/challenge-10.ts):

```javascript
function createChristmasTree(ornaments, height) {
  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'
}
```

Solution by [yasai](https://github.com/yasai):

```javascript
function createChristmasTree(ornaments, height) {
    let tree = "";
    let count = 0;

    // pyramid
    for (let i = 0; i < height; i++) {
      let line = "";
      // spaces
      line += " ".repeat(height - i - 1);
      // ornaments
      for (let j = 0; j < i + 1; j++) {
        let dec = ornaments[count];
        count++;
        if (count == ornaments.length) count = 0;
         if (j == i) {
          line += `${dec}`;
          break;
        }
        line += `${dec} `;
      }
      // add to the tree
      tree += line + "\n";
    }

    // trunk
    tree += " ".repeat(height - 1) + "|\n";

    return tree;
  }
```

And that was the challenge for December 10th and its solutions. Give it a like if you enjoyed the challenge or the solution! Do you have an alternative solution? Leave it in the comments!