satyacode
Background
Back to js-conditionals

04-weather-advice.js

JavaScript
1/**
2 * 🌤️ TrailBuddy - The Hiking Weather App
3 *
4 * You're building a weather advisory feature for TrailBuddy, a popular
5 * hiking app used by thousands of outdoor enthusiasts. Based on the
6 * temperature (in Celsius) and whether it's raining, the app should
7 * display helpful advice to hikers.
8 *
9 * Advisory Rules (check in this exact order):
10 *   - temp >= 35              → "Too hot for hiking - stay indoors and hydrate"
11 *   - temp >= 25 and no rain  → "Great weather for hiking - don't forget sunscreen"
12 *   - temp >= 25 and raining  → "Warm but rainy - consider indoor activities"
13 *   - temp >= 15 and no rain  → "Perfect hiking weather - enjoy the trails"
14 *   - temp >= 15 and raining  → "Cool and rainy - bring waterproof gear if hiking"
15 *   - temp >= 5 and no rain   → "Chilly - wear layers for your hike"
16 *   - temp >= 5 and raining   → "Cold and wet - best to stay indoors"
17 *   - temp < 5                → "Too cold - stay warm indoors"
18 *
19 * @param {number} temperature - Temperature in Celsius
20 * @param {boolean} isRaining - Whether it's currently raining
21 * @returns {string} The weather advisory message
22 */
23export function getWeatherAdvice(temperature, isRaining) {
24  let temp = temperature;
25  if(temp >= 35){
26    return "Too hot for hiking - stay indoors and hydrate"
27  }else if(temp >= 25){
28    return  isRaining? "Warm but rainy - consider indoor activities": "Great weather for hiking - don't forget sunscreen"
29  }else if(temp >= 15){
30    return isRaining ? "Cool and rainy - bring waterproof gear if hiking": "Perfect hiking weather - enjoy the trails" 
31  } else if(temp >= 5){
32    return isRaining ? "Cold and wet - best to stay indoors" : "Chilly - wear layers for your hike"
33  } else {
34    return "Too cold - stay warm indoors"
35  }
36}
37