Advertisement
Spocoman

10. Weather Forecast - Part 2

Dec 8th, 2021 (edited)
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function weatherForecast2 (input) {
  2.  
  3.     let degrees = Number(input[0]);
  4.     let weather = "";
  5.    
  6.     if (degrees >= 5 && degrees < 12) {
  7.         weather = "Cold";
  8.     } else if (degrees >= 12 && degrees < 15) {
  9.         weather = "Cool";
  10.     } else if (degrees >= 15 && degrees <= 20) {
  11.         weather = "Mild";
  12.     } else if (degrees > 20 && degrees < 26) {
  13.         weather = "Warm";
  14.     } else if (degrees >= 26 && degrees <= 35) {
  15.         weather = "Hot";
  16.     } else {
  17.         weather = "unknown";
  18.     }
  19.  
  20.     console.log(weather);
  21. }
  22.  
  23.  
  24. РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
  25.  
  26. function weatherForecast2(input) {
  27.  
  28.     let degrees = Number(input[0]);
  29.  
  30.     let weather =
  31.         degrees >= 5 && degrees < 12 ? "Cold" :
  32.         degrees >= 12 && degrees < 15 ? "Cool" :
  33.         degrees >= 15 && degrees <= 20 ? "Mild" :
  34.         degrees > 20 && degrees < 26 ? "Warm" :
  35.         degrees >= 26 && degrees <= 35 ? "Hot" : "unknown";
  36.  
  37.     console.log(weather);
  38. }
  39.  
  40.  
  41. ИЛИ:
  42.  
  43. function weatherForecast2(input) {
  44.     let degrees = Number(input[0]);
  45.  
  46.     console.log(
  47.         degrees >= 5 && degrees < 12 ?
  48.         "Cold" :
  49.         degrees >= 12 && degrees < 15 ?
  50.         "Cool" :
  51.         degrees >= 15 && degrees <= 20 ?
  52.         "Mild" :
  53.         degrees > 20 && degrees < 26 ?
  54.         "Warm" :
  55.         degrees >= 26 && degrees <= 35 ?
  56.         "Hot" :
  57.         "unknown"
  58.     );
  59. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement