Правильный ли у меня подход к решению задачи?

Есть такая функция, которая из Json берет id и вставляет букву иконочного шрифта;
нормально ли это делать через switch/case?

function showIcon(response) {
  var id = response.weather[0];
  for (var key in id) {
    if (key == "id") {
        id = id[key];
        switch (id) {
          case 200:
          case 201:
          case 202:
          case 210:
          case 211:
          case 212:
          case 221:
          case 230:
          case 231:
          case 232:
              return icon.setAttribute("data-icon", "P"); //Thunderlight
            break;
          case 300:
          case 301:
          case 302:
          case 310:
          case 311:
          case 312:
          case 313:
          case 314:
          case 321:
              return icon.setAttribute("data-icon", "Q") //Dizzle
              break;
          case 500:
          case 501:
          case 502:
          case 503:
          case 504:
          case 511:
          case 520:
          case 521:
          case 522:
          case 531:
              return icon.setAttribute("data-icon", "R"); //Rain
              break;
          case 600:
          case 601:
          case 602:
          case 611:
          case 612:
          case 615:
          case 616:
          case 620:
          case 621:
          case 622:
              return icon.setAttribute("data-icon", "W") // Snow
          case 701:
          case 711:
          case 721:
          case 731:
          case 741:
          case 751:
          case 761:
          case 762:
          case 771:
          case 781:
              return icon.setAttribute("data-icon", "M") // Fog
          case 800:
              return icon.setAttribute("data-icon", "B"); // Clear
              break;
          case 801:
              return icon.setAttribute("data-icon", "H"); // Few clouds
              break;
          case 802:
              return icon.setAttribute("data-icon", "N"); // Scattered clouds
              break;
          case 803:
          case 804:
              return icon.setAttribute("data-icon", "Y") // Broken clouds
          default:
              return icon.setAttribute("data-icon", ")") // N/A
        }
    }
  }
}
  • Вопрос задан
  • 187 просмотров
Пригласить эксперта
Ответы на вопрос 1
switch (true) {
  case (id >= 200 && id < 300): return icon.setAttribute("data-icon", "P");
  case (id >= 300 && id < 400): return icon.setAttribute("data-icon", "Q");
  case (id >=500 && id < 600): return icon.setAttribute("data-icon", "R");
  case (id >=600 && id < 700): return icon.setAttribute("data-icon", "W");
  case (id >= 700 && id < 800): return icon.setAttribute("data-icon", "M");
  case (id === 800): return icon.setAttribute("data-icon", "B");
  case (id === 801): return icon.setAttribute("data-icon", "H");
  case (id === 802): return icon.setAttribute("data-icon", "N");
  case (id === 803 || id === 804): return icon.setAttribute("data-icon", "Y");
  default: return icon.setAttribute("data-icon", ")");
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы