Skip to content

Commit 62deba6

Browse files
committedDec 18, 2024·
Add solution challenge 15
1 parent f3b5c46 commit 62deba6

File tree

6 files changed

+310
-0
lines changed

6 files changed

+310
-0
lines changed
 
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Reto #15: ✍️ Dibujando tablas
2+
3+
Al Polo Norte ha llegado ChatGPT y el elfo Sam Elfman está trabajando en una aplicación de administración de regalos y niños.
4+
5+
Para mejorar la presentación, quiere crear una función drawTable que reciba un array de objetos y lo convierta en una tabla de texto.
6+
7+
La tabla dibujada debe representar los datos del objeto de la siguiente manera:
8+
9+
- Tiene una cabecera con el nombre de la columna.
10+
- El nombre de la columna pone la primera letra en mayúscula.
11+
- Cada fila debe contener los valores de los objetos en el orden correspondiente.
12+
- Cada valor debe estar alineado a la izquierda.
13+
- Los campos dejan siempre un espacio a la izquierda.
14+
- Los campos dejan a la derecha el espacio necesario para alinear la caja.
15+
16+
Mira el ejemplo para ver cómo debes dibujar la tabla:
17+
18+
```js
19+
drawTable([
20+
{ name: 'Alice', city: 'London' },
21+
{ name: 'Bob', city: 'Paris' },
22+
{ name: 'Charlie', city: 'New York' }
23+
])
24+
// +---------+-----------+
25+
// | Name | City |
26+
// +---------+-----------+
27+
// | Alice | London |
28+
// | Bob | Paris |
29+
// | Charlie | New York |
30+
// +---------+-----------+
31+
32+
drawTable([
33+
{ gift: 'Doll', quantity: 10 },
34+
{ gift: 'Book', quantity: 5 },
35+
{ gift: 'Music CD', quantity: 1 }
36+
])
37+
// +----------+----------+
38+
// | Gift | Quantity |
39+
// +----------+----------+
40+
// | Doll | 10 |
41+
// | Book | 5 |
42+
// | Music CD | 1 |
43+
// +----------+----------+
44+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
presets: [
3+
[
4+
'@babel/preset-env',
5+
{
6+
targets: {
7+
node: 'current',
8+
},
9+
},
10+
],
11+
],
12+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export function minMovesToStables(reindeer, stables) {
2+
const stablesSort = stables.sort((a, b) => a - b)
3+
const reindeerSort = reindeer.sort((a, b) => a - b)
4+
5+
let result = 0
6+
7+
for (let i = 0; i < reindeer.length; i++) {
8+
const rein = reindeerSort[i]
9+
const stable = stablesSort[i]
10+
11+
result += Math.abs(rein - stable)
12+
}
13+
14+
return result
15+
}
16+
17+
const result = minMovesToStables([1, 1, 3], [1, 8, 4])
18+
console.log({ result })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* For a detailed explanation regarding each configuration property, visit:
3+
* https://jestjs.io/docs/configuration
4+
*/
5+
6+
/** @type {import('jest').Config} */
7+
const config = {
8+
// All imported modules in your tests should be mocked automatically
9+
// automock: false,
10+
11+
// Stop running tests after `n` failures
12+
// bail: 0,
13+
14+
// The directory where Jest should store its cached dependency information
15+
// cacheDirectory: "/tmp/jest_rs",
16+
17+
// Automatically clear mock calls, instances, contexts and results before every test
18+
clearMocks: true,
19+
20+
// Indicates whether the coverage information should be collected while executing the test
21+
// collectCoverage: false,
22+
23+
// An array of glob patterns indicating a set of files for which coverage information should be collected
24+
// collectCoverageFrom: undefined,
25+
26+
// The directory where Jest should output its coverage files
27+
// coverageDirectory: undefined,
28+
29+
// An array of regexp pattern strings used to skip coverage collection
30+
// coveragePathIgnorePatterns: [
31+
// "/node_modules/"
32+
// ],
33+
34+
// Indicates which provider should be used to instrument code for coverage
35+
coverageProvider: 'v8',
36+
37+
// A list of reporter names that Jest uses when writing coverage reports
38+
// coverageReporters: [
39+
// "json",
40+
// "text",
41+
// "lcov",
42+
// "clover"
43+
// ],
44+
45+
// An object that configures minimum threshold enforcement for coverage results
46+
// coverageThreshold: undefined,
47+
48+
// A path to a custom dependency extractor
49+
// dependencyExtractor: undefined,
50+
51+
// Make calling deprecated APIs throw helpful error messages
52+
// errorOnDeprecated: false,
53+
54+
// The default configuration for fake timers
55+
// fakeTimers: {
56+
// "enableGlobally": false
57+
// },
58+
59+
// Force coverage collection from ignored files using an array of glob patterns
60+
// forceCoverageMatch: [],
61+
62+
// A path to a module which exports an async function that is triggered once before all test suites
63+
// globalSetup: undefined,
64+
65+
// A path to a module which exports an async function that is triggered once after all test suites
66+
// globalTeardown: undefined,
67+
68+
// A set of global variables that need to be available in all test environments
69+
// globals: {},
70+
71+
// The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers.
72+
// maxWorkers: "50%",
73+
74+
// An array of directory names to be searched recursively up from the requiring module's location
75+
// moduleDirectories: [
76+
// "node_modules"
77+
// ],
78+
79+
// An array of file extensions your modules use
80+
moduleFileExtensions: ['js', 'mjs', 'cjs', 'jsx', 'ts', 'tsx', 'json', 'node'],
81+
82+
// A map from regular expressions to module names or to arrays of module names that allow to stub out resources with a single module
83+
// moduleNameMapper: {},
84+
85+
// An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader
86+
// modulePathIgnorePatterns: [],
87+
88+
// Activates notifications for test results
89+
// notify: false,
90+
91+
// An enum that specifies notification mode. Requires { notify: true }
92+
// notifyMode: "failure-change",
93+
94+
// A preset that is used as a base for Jest's configuration
95+
// preset: undefined,
96+
97+
// Run tests from one or more projects
98+
// projects: undefined,
99+
100+
// Use this configuration option to add custom reporters to Jest
101+
// reporters: undefined,
102+
103+
// Automatically reset mock state before every test
104+
// resetMocks: false,
105+
106+
// Reset the module registry before running each individual test
107+
// resetModules: false,
108+
109+
// A path to a custom resolver
110+
// resolver: undefined,
111+
112+
// Automatically restore mock state and implementation before every test
113+
// restoreMocks: false,
114+
115+
// The root directory that Jest should scan for tests and modules within
116+
// rootDir: undefined,
117+
118+
// A list of paths to directories that Jest should use to search for files in
119+
roots: ['<rootDir>'],
120+
121+
// Allows you to use a custom runner instead of Jest's default test runner
122+
// runner: "jest-runner",
123+
124+
// The paths to modules that run some code to configure or set up the testing environment before each test
125+
// setupFiles: [],
126+
127+
// A list of paths to modules that run some code to configure or set up the testing framework before each test
128+
// setupFilesAfterEnv: [],
129+
130+
// The number of seconds after which a test is considered as slow and reported as such in the results.
131+
// slowTestThreshold: 5,
132+
133+
// A list of paths to snapshot serializer modules Jest should use for snapshot testing
134+
// snapshotSerializers: [],
135+
136+
// The test environment that will be used for testing
137+
// testEnvironment: "jest-environment-node",
138+
139+
// Options that will be passed to the testEnvironment
140+
// testEnvironmentOptions: {},
141+
142+
// Adds a location field to test results
143+
// testLocationInResults: false,
144+
145+
// The glob patterns Jest uses to detect test files
146+
testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[tj]s?(x)'],
147+
148+
// An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
149+
testPathIgnorePatterns: ['/node_modules/'],
150+
151+
// The regexp pattern or array of patterns that Jest uses to detect test files
152+
// testRegex: [],
153+
154+
// This option allows the use of a custom results processor
155+
// testResultsProcessor: undefined,
156+
157+
// This option allows use of a custom test runner
158+
// testRunner: "jest-circus/runner",
159+
160+
// A map from regular expressions to paths to transformers
161+
transform: {
162+
'\\.[jt]sx?$': 'babel-jest',
163+
},
164+
165+
// An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation
166+
// transformIgnorePatterns: [
167+
// "/node_modules/",
168+
// "\\.pnp\\.[^\\/]+$"
169+
// ],
170+
171+
// An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them
172+
// unmockedModulePathPatterns: undefined,
173+
174+
// Indicates whether each individual test should be reported during the run
175+
// verbose: undefined,
176+
177+
// An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode
178+
// watchPathIgnorePatterns: [],
179+
180+
// Whether to use watchman for file crawling
181+
// watchman: true,
182+
}
183+
184+
module.exports = config
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "challenge-2024-challenge-15",
3+
"version": "1.0.0",
4+
"description": "Reto #15: ✍️ Dibujando tablas",
5+
"main": "drawTable.js",
6+
"type": "module",
7+
"scripts": {
8+
"prod": "node drawTable.js",
9+
"test": "jest"
10+
},
11+
"keywords": [
12+
"adventjs",
13+
"challenge",
14+
"desafíos",
15+
"retos",
16+
"JavaScript",
17+
"programación",
18+
"navidad"
19+
],
20+
"author": "John Serrano",
21+
"license": "ISC",
22+
"devDependencies": {
23+
"@babel/core": "7.23.5",
24+
"@babel/preset-env": "7.23.5",
25+
"babel-jest": "29.7.0",
26+
"@types/jest": "29.5.10",
27+
"jest": "29.7.0"
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { drawTable } from '../drawTable'
2+
describe('Files names', () => {
3+
test('Should return an string', () => {
4+
const result = drawTable([
5+
{ name: 'Alice', city: 'London' },
6+
{ name: 'Bob', city: 'Paris' },
7+
{ name: 'Charlie', city: 'New York' },
8+
])
9+
10+
// +---------+-----------+
11+
// | Name | City |
12+
// +---------+-----------+
13+
// | Alice | London |
14+
// | Bob | Paris |
15+
// | Charlie | New York |
16+
// +---------+-----------+
17+
18+
expect(result).toBeDefined()
19+
expect(result).toBe(
20+
'+---------+-----------+\n| Name | City |\n+---------+-----------+\n| Alice | London |\n| Bob | Paris |\n| Charlie | New York |\n+---------+-----------+'
21+
)
22+
})
23+
})

0 commit comments

Comments
 (0)
Please sign in to comment.