diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index b22590bc6..d55963dd9 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -6,9 +6,22 @@ // or 'list' has mixed values (the function is expected to sort only numbers). function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!Array.isArray(list)) { + return null; + } + const numbersOnly = list.filter((item) => typeof item === "number"); + if (numbersOnly.length === 0) { + return null; + } + const sortedNumbers = [...numbersOnly].sort((a, b) => a - b); + + const len = sortedNumbers.length; + const middleIndex = Math.floor(len / 2); + if (len % 2 !== 0) { + return sortedNumbers[middleIndex]; + } else { + return (sortedNumbers[middleIndex - 1] + sortedNumbers[middleIndex]) / 2; + } } module.exports = calculateMedian; diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718a..6bd57109d 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,8 @@ -function dedupe() {} +function dedupe(list) { + if (!Array.isArray(list)) { + return list; + } + return [...new Set(list)]; +} + +module.exports = dedupe; diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378e..a0c6b882c 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,9 @@ function findMax(elements) { + const numbersOnly = elements.filter((item) => typeof item === "number"); + if (numbersOnly.length === 0) { + return -Infinity; + } + return Math.max(...numbersOnly); } module.exports = findMax; diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe3..57051a30d 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,6 @@ -function sum(elements) { +function sum(numbers) { + const numbersOnly = numbers.filter((item) => typeof item === "number"); + return numbersOnly.reduce((total, current) => total + current, 0); } module.exports = sum; diff --git a/Sprint-1/package-lock.json b/Sprint-1/package-lock.json index 83e427d0b..b52480af5 100644 --- a/Sprint-1/package-lock.json +++ b/Sprint-1/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f0..a1a53ff8f 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,8 +1,7 @@ // Refactor the implementation of includes to use a for...of loop function includes(list, target) { - for (let index = 0; index < list.length; index++) { - const element = list[index]; + for (const element of list) { if (element === target) { return true; } @@ -10,4 +9,4 @@ function includes(list, target) { return false; } -module.exports = includes; +module.exports = includes; \ No newline at end of file diff --git a/Sprint-1/stretch/aoc-2018-day1/solution.js b/Sprint-1/stretch/aoc-2018-day1/solution.js index e69de29bb..8e923e3cd 100644 --- a/Sprint-1/stretch/aoc-2018-day1/solution.js +++ b/Sprint-1/stretch/aoc-2018-day1/solution.js @@ -0,0 +1,7 @@ +function calibrate(input) { + return input.reduce((total, change) => { + return total + parseInt(change, 10); + }, 0); +} + +module.exports = calibrate; \ No newline at end of file diff --git a/Sprint-3/alarmclock/alarmclock.js b/Sprint-3/alarmclock/alarmclock.js index 6ca81cd3b..555358616 100644 --- a/Sprint-3/alarmclock/alarmclock.js +++ b/Sprint-3/alarmclock/alarmclock.js @@ -1,4 +1,42 @@ -function setAlarm() {} +let timerInterval; + +function setAlarm() { + const alarmInput = document.getElementById("alarmSet"); + const timeDisplay = document.getElementById("timeRemaining"); + + let totalSeconds = parseInt(alarmInput.value); + + if (!totalSeconds) { + return; + } + + clearInterval(timerInterval); + + function updateTimerDisplay(seconds) { + const minutes = Math.floor(seconds / 60); + const remainingSeconds = seconds % 60; + + const displayMinutes = minutes < 10 ? "0" + minutes : minutes; + const displaySeconds = + remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds; + + timeDisplay.innerText = + "Time Remaining: " + displayMinutes + ":" + displaySeconds; + } + + updateTimerDisplay(totalSeconds); + + timerInterval = setInterval(() => { + totalSeconds--; + updateTimerDisplay(totalSeconds); + + if (totalSeconds <= 0) { + clearInterval(timerInterval); + playAlarm(); + document.body.style.backgroundColor = "red"; + } + }, 1000); +} // DO NOT EDIT BELOW HERE @@ -20,6 +58,8 @@ function playAlarm() { function pauseAlarm() { audio.pause(); + clearInterval(timerInterval); + document.body.style.backgroundColor = "white"; } window.onload = setup; diff --git a/Sprint-3/quote-generator/quotes.js b/Sprint-3/quote-generator/quotes.js index 4a4d04b72..f9cd271c9 100644 --- a/Sprint-3/quote-generator/quotes.js +++ b/Sprint-3/quote-generator/quotes.js @@ -491,3 +491,33 @@ const quotes = [ ]; // call pickFromArray with the quotes array to check you get a random quote +// Код для генерации и отображения цитаты +function showNewQuote() { + // Используем функцию pickFromArray, которая уже есть в файле + const randomQuote = pickFromArray(quotes); + + // Находим элементы (ID "quote" и "author" важны для прохождения тестов) + const quoteDisplay = document.getElementById("quote"); + const authorDisplay = document.getElementById("author"); + + // Проверяем, что элементы найдены, и обновляем их текст + if (quoteDisplay && authorDisplay) { + quoteDisplay.innerText = randomQuote.quote; + authorDisplay.innerText = randomQuote.author; + } +} + +// Настройка приложения при загрузке страницы +function setup() { + // Показываем первую случайную цитату сразу (Acceptance Criterion #1) + showNewQuote(); + + // Находим кнопку и вешаем событие клика (Acceptance Criterion #2) + const newQuoteBtn = document.getElementById("new-quote"); + if (newQuoteBtn) { + newQuoteBtn.addEventListener("click", showNewQuote); + } +} + +// Вызываем setup, когда страница полностью загрузится +window.onload = setup;