Untitled

 avatar
unknown
plain_text
20 days ago
1.2 kB
2
Indexable
function solution1(expenses) {
    let allExpenses = [];

    // Przechodzimy przez każdy miesiąc
    for (const month in expenses) {
        const days = expenses[month];

        // Przechodzimy przez każdy dzień w miesiącu
        for (const day in days) {
            const categories = days[day];

            // Sprawdzamy, czy dzień jest pierwszą niedzielą lub wcześniejszy
            const date = new Date(`${month}-${day}`);
            if (date.getDay() === 0 || date.getDate() <= 7) {
                // Dodajemy wszystkie wydatki do listy
                for (const category in categories) {
                    allExpenses = allExpenses.concat(categories[category]);
                }
            }
        }
    }

    // Jeśli nie ma wydatków, zwracamy null
    if (allExpenses.length === 0) return null;

    // Sortujemy wydatki
    allExpenses.sort((a, b) => a - b);

    // Obliczamy medianę
    const mid = Math.floor(allExpenses.length / 2);
    if (allExpenses.length % 2 === 0) {
        return (allExpenses[mid - 1] + allExpenses[mid]) / 2;
    } else {
        return allExpenses[mid];
    }
}
Leave a Comment