vendredi 24 septembre 2021

How to reduce complexity of 27 by replacing if/else block to something else in React

I have a weather display with emojis adjusted by if/else statement, but there is a major delay evidently since the complexity is 27, how do I replace such method with a replacement that would reduce complexity?

Sort of new to front-end world, any help is much appreciated.

  function drawWeather( d ) {
  var celcius = Math.round(parseFloat(d.main.temp)-273.15);
  var fahrenheit = 
  Math.round(((parseFloat(d.main.temp)-273.15)*1.8)+32);
  var main_description = d.weather[0].main; 
  var description = d.weather[0].description; 

  var day_time = isDay()

  document.getElementById('location').innerHTML = d.name;

  if       ( main_description === 'Clear' && day_time == true) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' ☀️';
  } else if ( main_description === 'Clear' && day_time == false) 
  {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌔';
  } else if( main_description === 'Clouds' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' ☁️';
  } else if( main_description === 'Drizzle' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌦️';
  } else if( main_description === 'Rain' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌧️';
  } else if( main_description === 'Thunderstorm' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' ⛈️';
  } else if( main_description === 'Snow' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' ❄️';
  } else if( main_description === 'Fog' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌫️';
  } else if( main_description === 'Mist' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌫️';
  } else if( main_description === 'Haze' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' + ' 🌫️';
  } else if( main_description === 'Tornado' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' 
  + ' 🌫️';
  } else if( main_description === 'Dust' ) {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°' 
   + ' 🌫️';
  } else {
  document.getElementById('temp').innerHTML = fahrenheit + 
  '°';
  }
  }

Aucun commentaire:

Enregistrer un commentaire