Skip to content

2024 02 #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ module.exports = {
'no-param-reassign': 'off',
'no-unused-expressions': 'off',
'no-return-assign': 'off',
'operator-linebreak': ['error', 'before'],
},
};
41 changes: 41 additions & 0 deletions 2024/02-enmarcando-nombres/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Reto 02: Enmarcando-nombres

**Santa Claus** 🎅 quiere enmarcar los nombres de los niños buenos para decorar su taller 🖼️, pero el marco debe cumplir unas reglas específicas. Tu tarea es ayudar a los elfos a generar este marco mágico.

**Reglas:**

Dado un array de nombres, debes crear un marco rectangular que los contenga a todos.
Cada nombre debe estar en una línea, alineado a la izquierda.
El marco está construido con * y tiene un borde de una línea de ancho.
La anchura del marco se adapta automáticamente al nombre más largo más un margen de 1 espacio a cada lado.

Ejemplo de funcionamiento:

```js
createFrame(['midu', 'madeval', 'educalvolpz'])

// Resultado esperado:
***************
* midu *
* madeval *
* educalvolpz *
***************

createFrame(['midu'])

// Resultado esperado:
********
* midu *
********

createFrame(['a', 'bb', 'ccc'])

// Resultado esperado:
*******
* a *
* bb *
* ccc *
*******

createFrame(['a', 'bb', 'ccc', 'dddd'])
```
9 changes: 9 additions & 0 deletions 2024/02-enmarcando-nombres/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function createFrame(names) {
const maxLength = Math.max(...names.map((name) => name.length));
const border = '*'.repeat(maxLength + 4);
const framedNames = names.map((name) => `* ${name.padEnd(maxLength, ' ')} *`);

return [border, ...framedNames, border].join('\n');
}

module.exports = createFrame;
36 changes: 36 additions & 0 deletions 2024/02-enmarcando-nombres/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const createFrame = require('./index');

describe('02 => Enmarcando-nombres', () => {
const TEST_CASES = [
{
input: ['midu'],
output: '********\n* midu *\n********',
},
{
input: ['midu', 'madeval', 'educalvolpz'],
output:
'***************\n* midu *\n* madeval *\n* educalvolpz *\n***************',
},
{
input: ['a', 'bb', 'ccc'],
output: '*******\n* a *\n* bb *\n* ccc *\n*******',
},
{
input: ['midu', 'madeval', 'educalvolpz', 'midu'],
output:
'***************\n* midu *\n* madeval *\n* educalvolpz *\n* midu *\n***************',
},
];

it('should return an array type', () => {
const { input } = TEST_CASES[0];
expect(typeof createFrame(input)).toBe('string');
});

it.each(TEST_CASES)(
'should return the correct frame',
({ input, output }) => {
expect(createFrame(input)).toBe(output);
},
);
});
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ npm run test 'year'/'challenge'/index.test.js
<details open>
<summary>Show / Hide</summary>

| # | Challenge | Difficulty | My Solution | My Score |
| :-: | ------------------------------------------------------------------------------------------- | :--------: | :------------------------------------------------------------------------------------------------------------: | :-----------: |
| 01 | [🎁 ¡Primer regalo repetido!](https://adventjs.dev/es/challenges/2024/1) | 🟢 | [here](./2024/01-primer-regalo-repetido/index.js) | ⭐⭐⭐⭐⭐ |
| # | Challenge | Difficulty | My Solution | My Score |
| :-: | ------------------------------------------------------------------------------------------- | :--------: | :--------------------------------------------------------------------------------------------------------: | :-----------: |
| 01 | [🎁 ¡Primer regalo repetido!](https://adventjs.dev/es/challenges/2024/1) | 🟢 | [here](./2024/01-primer-regalo-repetido/index.js) | ⭐⭐⭐⭐⭐ |
| 02 | [🖼️ Enmarcando nombres](https://adventjs.dev/es/challenges/2024/2) | 🟢 | [here](./2024/02-enmarcando-nombres/index.js) | ⭐⭐⭐⭐⭐ |

Difficulties legend:
🟢 Easy 🟡 Medium 🔴 Hard
Expand Down