Skip to content

Instantly share code, notes, and snippets.

@call0fcode
Created May 19, 2025 11:16
Show Gist options
  • Save call0fcode/b7c5db7aeacd2ece8157c1d193da900d to your computer and use it in GitHub Desktop.
Save call0fcode/b7c5db7aeacd2ece8157c1d193da900d to your computer and use it in GitHub Desktop.
// Solución nivel fácil
export function sumNumbers(numbers: number[]):number {
return numbers.reduce((sum, num) => sum + num, 0);
};
// Solución nivel medio
export function sumNumbers(numbers: number[]):number {
return numbers
.filter(Number.isInteger)
.reduce((sum, num) => sum + num, 0);
};
// Solución nivel difícil
export function sumNumbers(numbers: (number | number[])[]):number {
return numbers
.flat(1)
.filter(Number.isInteger)
.reduce((sum, num) => sum + num, 0);
};
// ¿Por qué falla esta prueba?
// 31 de abril no existe.
describe('Fácil', () => {
it('debe retornar true para una fecha válida', () => {
expect(validateDateFormat("31/04/2021")).toBe(true);
});
});
// ¿Por qué falla esta prueba?
// El mes tiene un formato incorrecto. Debe ser 02 en vez de 2.
describe.skip('Medio', () => {
it('debe retornar true para una fecha válida', () => {
expect(validateDateFormat("01/2/2021")).toBe(true);
});
});
// ¿Por qué falla esta prueba?
// - El año no es bisiesto. Bisiesto sería p.ej. 2024
// - Boolean no es un tipo a comparar, debe ser el string "boolean"
describe.skip('Difícil', () => {
it('debe validar correctamente una fecha de año bisiesto', () => {
const result = validateDateFormat("29/02/2100");
expect(typeof result).toBe(Boolean);
expect(result).toBeTruthy();
});
});
// Fácil
export function countWords(text: string): Record<string, number> {
// Split the text into words by spaces (case-sensitive, no punctuation handling)
const words = text.split(' ').filter(Boolean);
const count: Record<string, number> = {};
// Count word occurrences
for (const word of words) {
count[word] = (count[word] || 0) + 1;
}
return count;
}
// Medio
export function countWords(text: string): Record<string, number> {
// Convert to lowercase and remove punctuation -> adicional
const cleanedText = text.toLowerCase().replace(/[^a-z0-9\s]/g, '');
// Split the text into words and filter out empty strings
const words = cleanedText.split(/\s+/).filter(Boolean);
const count: Record<string, number> = {};
// Count word occurrences
for (const word of words) {
count[word] = (count[word] || 0) + 1;
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment