Skip to content

Commit

Permalink
✨ add challenge-13 solution
Browse files Browse the repository at this point in the history
  • Loading branch information
marcode24 committed Dec 14, 2023
1 parent 40476b5 commit 6ea18fb
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
51 changes: 51 additions & 0 deletions 2023/13-calculando-el-tiempo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Reto 13: Calculando el tiempo

## Problema

Los elfos están preparando **la víspera de Navidad** y necesitan tu ayuda para calcular si van sobrados o no de tiempo ⏳.

Para ello te pasan un array con la duración de cada entrega. El formato de la duración es HH:mm:ss, las entregas empiezan a las 00:00:00 y el límite de tiempo es 07:00:00.

**Tu función debe devolver el tiempo que les faltará o el tiempo que les sobrará** para terminar las entregas. El formato de la duración devuelta debe ser HH:mm:ss.

Si terminan antes de las 07:00:00, el tiempo restante hasta las 07:00:00 debe ser mostrado con un signo negativo. Por ejemplo, **si sobran 1 hora y 30 minutos, devuelve -01:30:00**

```js
calculateTime(['00:10:00', '01:00:00', '03:30:00'])
// '-02:20:00'

calculateTime(['02:00:00', '05:00:00', '00:30:00'])
// '00:30:00'

calculateTime([
'00:45:00',
'00:45:00',
'00:00:30',
'00:00:30'
]) // '-05:29:00'
```

## Mi solución

```js
function calculateTime(deliveries) {
const deliveryLimit = 7 * 3600; // Límite de tiempo en segundos (7 horas)
let totalSeconds = 0;

// eslint-disable-next-line no-restricted-syntax
for (const time of deliveries) {
const [hours, minutes, seconds] = time.split(':').map(Number);
totalSeconds += hours * 3600 + minutes * 60 + seconds;
}

const remainingSeconds = deliveryLimit - totalSeconds;
const pad = (value) => (value < 10 ? `0${value}` : `${value}`);
const sign = remainingSeconds <= 0 ? '' : '-';
const absRemainingSeconds = Math.abs(remainingSeconds);
const resultHours = pad(Math.floor(absRemainingSeconds / 3600));
const resultMinutes = pad(Math.floor((absRemainingSeconds % 3600) / 60));
const resultSeconds = pad(absRemainingSeconds % 60);
return `${sign}${resultHours}:${resultMinutes}`
+ `:${resultSeconds}`;
}
```
22 changes: 22 additions & 0 deletions 2023/13-calculando-el-tiempo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function calculateTime(deliveries) {
const deliveryLimit = 7 * 3600; // Límite de tiempo en segundos (7 horas)
let totalSeconds = 0;

// eslint-disable-next-line no-restricted-syntax
for (const time of deliveries) {
const [hours, minutes, seconds] = time.split(':').map(Number);
totalSeconds += hours * 3600 + minutes * 60 + seconds;
}

const remainingSeconds = deliveryLimit - totalSeconds;
const pad = (value) => (value < 10 ? `0${value}` : `${value}`);
const sign = remainingSeconds <= 0 ? '' : '-';
const absRemainingSeconds = Math.abs(remainingSeconds);
const resultHours = pad(Math.floor(absRemainingSeconds / 3600));
const resultMinutes = pad(Math.floor((absRemainingSeconds % 3600) / 60));
const resultSeconds = pad(absRemainingSeconds % 60);
return `${sign}${resultHours}:${resultMinutes}`
+ `:${resultSeconds}`;
}

module.exports = calculateTime;
26 changes: 26 additions & 0 deletions 2023/13-calculando-el-tiempo/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const calculateTime = require('./index');

describe('13 => Calculando el tiempo', () => {
const testCases = [
{
input: ['00:10:00', '01:00:00', '03:30:00'],
output: '-02:20:00',
},
{
input: ['02:00:00', '05:00:00', '00:30:00'],
output: '00:30:00',
},
{
input: ['00:45:00', '00:45:00', '00:00:30', '00:00:30'],
output: '-05:29:00',
},
];

it('should return a string type', () => {
expect(typeof calculateTime(testCases[0].input)).toBe('string');
});

it.each(testCases)('should return $output', (testCase) => {
expect(calculateTime(testCase.input)).toBe(testCase.output);
});
});

0 comments on commit 6ea18fb

Please sign in to comment.