satyacode
Background
Back to js-conditionals

12-season-activity.js

JavaScript
1/**
2 * πŸ—ΊοΈ WanderLust Travel Planner
3 *
4 * WanderLust is a travel planning app that suggests fun activities
5 * based on the month and the current temperature. Users enter the
6 * month number and temperature, and the app recommends what to do!
7 *
8 * Step 1 β€” Determine the season from the month:
9 *   - December, January, February  (12, 1, 2)   β†’ "Winter"
10 *   - March, April, May            (3, 4, 5)     β†’ "Spring"
11 *   - June, July, August           (6, 7, 8)     β†’ "Summer"
12 *   - September, October, November (9, 10, 11)   β†’ "Autumn"
13 *
14 * Step 2 β€” Suggest an activity based on season AND temperature (Β°C):
15 *   - Winter + temp < 0     β†’ "skiing"
16 *   - Winter + temp >= 0    β†’ "ice skating"
17 *   - Spring + temp > 20    β†’ "hiking"
18 *   - Spring + temp <= 20   β†’ "museum visit"
19 *   - Summer + temp > 35    β†’ "swimming"
20 *   - Summer + temp <= 35   β†’ "cycling"
21 *   - Autumn + temp > 15    β†’ "nature walk"
22 *   - Autumn + temp <= 15   β†’ "reading at a cafe"
23 *
24 * Return an object: { season: string, activity: string }
25 *
26 * Rules:
27 *   - If month is not 1–12, return null
28 *
29 * @param {number} month - Month of the year (1-12)
30 * @param {number} temperature - Current temperature in Celsius
31 * @returns {{ season: string, activity: string } | null}
32 */
33export function getSeasonActivity(month, temperature) {
34  if (!Number.isInteger(month) || month < 1 || month > 12) {
35    return null;
36  }
37
38
39  function getSeason(month) {
40    if ([12, 1, 2].includes(month)) {
41      return "Winter"
42    }
43    if ([3, 4, 5].includes(month)) {
44      return "Spring"
45    }
46    if ([6, 7, 8].includes(month)) {
47      return "Summer"
48    }
49    if ([9, 10, 11].includes(month)) {
50      return "Autumn"
51    }
52  }
53
54  function getActivity(season, temp) {
55    if(season === "Winter"){
56        return temp < 0 ? "skiing": "ice skating" 
57    }
58
59    if(season === "Spring"){
60        return temp > 20? "hiking": "museum visit"
61    }
62
63    if(season == "Summer"){
64        return temp > 35? "swimming" : "cycling"
65    }
66
67    if(season == "Autumn"){
68        return temp > 15? "nature walk" : "reading at a cafe"
69    }
70  }
71
72    let season = getSeason(month)
73    let activity = getActivity(season, temperature)
74    
75  return {season, activity}
76}
77