diff --git a/.eslintrc.js b/.eslintrc.js index 8840319..7517ee0 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -17,5 +17,6 @@ module.exports = { 'no-param-reassign': 'off', 'no-unused-expressions': 'off', 'no-return-assign': 'off', + 'operator-linebreak': ['error', 'before'], }, }; diff --git a/2024/02-enmarcando-nombres/README.md b/2024/02-enmarcando-nombres/README.md new file mode 100644 index 0000000..cf00f0b --- /dev/null +++ b/2024/02-enmarcando-nombres/README.md @@ -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']) +``` diff --git a/2024/02-enmarcando-nombres/index.js b/2024/02-enmarcando-nombres/index.js new file mode 100644 index 0000000..6a544ed --- /dev/null +++ b/2024/02-enmarcando-nombres/index.js @@ -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; diff --git a/2024/02-enmarcando-nombres/index.test.js b/2024/02-enmarcando-nombres/index.test.js new file mode 100644 index 0000000..c4a3e2b --- /dev/null +++ b/2024/02-enmarcando-nombres/index.test.js @@ -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); + }, + ); +}); diff --git a/README.md b/README.md index a5390ad..907e1a8 100644 --- a/README.md +++ b/README.md @@ -57,9 +57,10 @@ npm run test 'year'/'challenge'/index.test.js
Show / Hide -| # | 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