From a66ed834f96cc0e8b8adb07dd43177f9d5de27ac Mon Sep 17 00:00:00 2001 From: Durga Prasad <65699516+dpgaharwal@users.noreply.github.com> Date: Tue, 7 Jan 2025 14:49:25 +0530 Subject: [PATCH] Handled NaN validation in Weather Calculator (#1977) --- Calculators/Weather-Calculator/script.js | 32 ++++++++++++++---------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/Calculators/Weather-Calculator/script.js b/Calculators/Weather-Calculator/script.js index c5b2e6256..8c72243e2 100644 --- a/Calculators/Weather-Calculator/script.js +++ b/Calculators/Weather-Calculator/script.js @@ -1,8 +1,19 @@ -document.getElementById('calculate').addEventListener('click', function() { - const temperature = parseFloat(document.getElementById('temperature').value); - const humidity = parseFloat(document.getElementById('humidity').value); - const windSpeed = parseFloat(document.getElementById('windSpeed').value); - +document.getElementById('calculate').addEventListener('click', function () { + const temperatureInput = document.getElementById('temperature').value; + const humidityInput = document.getElementById('humidity').value; + const windSpeedInput = document.getElementById('windSpeed').value; + + // Check for empty inputs + if (!temperatureInput || !humidityInput || !windSpeedInput) { + alert("Please fill in all the fields: Temperature, Humidity, and Wind Speed."); + return; + } + + const temperature = parseFloat(temperatureInput); + const humidity = parseFloat(humidityInput); + const windSpeed = parseFloat(windSpeedInput); + + // Perform calculations and display results const results = `

Temperature Conversion: ${convertTemperature(temperature)} °F

Wind Chill: ${findWindChill(temperature, windSpeed)} °C

@@ -10,17 +21,15 @@ document.getElementById('calculate').addEventListener('click', function() {

Heat Index: ${findHeatIndex(temperature, humidity)} °C

Human Thermal Comfort: ${findHumanThermalComfort(temperature, humidity, windSpeed)}

`; - + document.getElementById('results').innerHTML = results; }); function convertTemperature(celsius) { - - return (celsius * 9/5) + 32; + return (celsius * 9/5) + 32; } function findWindChill(temperature, windSpeed) { - if (temperature > 10 || windSpeed < 4.8) { return "❌"; } @@ -28,16 +37,14 @@ function findWindChill(temperature, windSpeed) { } function findDewPoint(temperature, humidity) { - const a = 17.27; const b = 237.7; const alpha = ((a * temperature) / (b + temperature)) + Math.log(humidity / 100); const dewPoint = (b * alpha) / (a - alpha); - return dewPoint.toFixed(2); + return dewPoint.toFixed(2); } function findHeatIndex(temperature, humidity) { - const T = temperature; const R = humidity; const HI = -8.78469475556 + 1.61139411 * T + 2.33854883889 * R + -0.14611605 * T * R + -0.012308094 * Math.pow(T, 2) + -0.0164248277778 * Math.pow(R, 2) + 0.002211732 * Math.pow(T, 2) * R + 0.00072546 * T * Math.pow(R, 2) + -0.000003582 * Math.pow(T, 2) * Math.pow(R, 2); @@ -45,7 +52,6 @@ function findHeatIndex(temperature, humidity) { } function findHumanThermalComfort(temperature, humidity, windSpeed) { - const THI = temperature - ((0.55 - (0.55 * (humidity / 100))) * (temperature - 14.5)); if (THI < 15) { return "Cold";