From 0503224816f1beb16db199ce5711014fa326cd82 Mon Sep 17 00:00:00 2001 From: saaymary Date: Wed, 7 Aug 2024 15:46:39 +0300 Subject: [PATCH] =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=B4=D1=81=D0=BA=D0=B0=D0=B7=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D1=8F=20=D0=BD=D0=B0=20js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/script.js b/script.js index 4e2ff72..56a89e9 100644 --- a/script.js +++ b/script.js @@ -13,3 +13,42 @@ /* При генерации нового предсказания старое предсказание должно добавляться в начало списка «Мои предсказания» — .forecasts */ /* Для добавления предсказания в список воспользуйся шаблоном forecast-item */ + + + +const predictions = [ + "Все будет супер!", + "Все будет еще лучше!", + "Затвра будет лучше, чем вчера!", + "Вскоре ты встретишь старого друга, и это будет замечательная встреча!", + "У тебя будет возможность реализовать свою мечту!", +]; + +function getRandomNumber(min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +const forecastBtn = document.querySelector('.forecast-btn'); +const currentForecastTitle = document.querySelector('.current-forecast h1'); +const currentForecastParagraph = document.querySelector('.current-forecast p'); +const forecastsContainer = document.querySelector('.forecasts'); +const forecastTemplate = document.getElementById('forecast-item').content; + +forecastBtn.addEventListener('click', () => { + const predictionIndex = getRandomNumber(0, predictions.length - 1); + const predictionText = predictions[predictionIndex]; + const successProbability = getRandomNumber(0, 100); + + currentForecastTitle.textContent = predictionText; + currentForecastParagraph.textContent = `Вероятность: ${successProbability}%`; + + addForecastToList(predictionText, successProbability); +}); + +function addForecastToList(predictionText, successProbability) { + const forecastItem = document.importNode(forecastTemplate, true); + forecastItem.querySelector('h3').textContent = predictionText; + forecastItem.querySelector('p').textContent = `Вероятность: ${successProbability}%`; + + forecastsContainer.prepend(forecastItem); +} \ No newline at end of file