Advertisement
braveheart1989

Forecast

Nov 23rd, 2016
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     const baseUrl = 'https://judgetests.firebaseio.com/locations.json';
  3.     const weatherUrl = 'https://judgetests.firebaseio.com/forecast/today/';
  4.     const upcomingWeatherUrl = 'https://judgetests.firebaseio.com/forecast/upcoming/';
  5.     $('#submit').click(GetWeather);
  6.  
  7.     function displayError() {
  8.         alert('error')
  9.     }
  10.  
  11.     function GetWeather() {
  12.         $('#forecast').css('display','block');
  13.         let code = '';
  14.  
  15.         $.get(baseUrl).then((weatherData)=> {
  16.             let location = $('#location').val();
  17.             for (let data of weatherData) {
  18.                 if(location == data.name) {
  19.                     code=data.code;
  20.                 }
  21.             }
  22.             $.get(weatherUrl + `${code}.json`).then((data)=> {
  23.                 let condWeather = '';
  24.                 if(data.forecast.condition=="Sunny") {
  25.                     condWeather = '☀';
  26.                 }else if(data.forecast.condition=="Partly sunny") {
  27.                     condWeather = '⛅';
  28.                 }else if(data.forecast.condition=="Overcast") {
  29.                     condWeather = '☁';
  30.                 }
  31.                 else if(data.forecast.condition=="Rain") {
  32.                     condWeather = '☂';
  33.                 }
  34.  
  35.  
  36.                 let conditionSymbol = $('<span class="condition symbol">').text(`${condWeather}`);
  37.  
  38.                 $('#current').find('.label').after(conditionSymbol);
  39.                 let condition = $('<span class="condition"></span>');
  40.                 let name = data.name;
  41.                 let townName = $('<span class="forecast-data">').text(name);
  42.                 let highLow = $('<span class="forecast-data">').text(`${data.forecast.low}°/${data.forecast.high}°`);
  43.                 let forecast = $('<span class="forecast-data">').text(data.forecast.condition);
  44.                 condition.append(townName);
  45.                 condition.append(highLow);
  46.                 condition.append(forecast);
  47.                 $(conditionSymbol).after(condition);
  48.  
  49.                 $.get(upcomingWeatherUrl + `${code}.json`).then((data)=> {
  50.                     //console.dir(data)
  51.                     // console.dir(d['condition']);
  52.                     // console.dir(d['high']);
  53.                     // console.dir(d['low']);
  54.  
  55.                     for (let d of data['forecast']) {
  56.                         if(d['condition']=="Sunny") {
  57.                             condWeather = '☀';
  58.                         }
  59.                         else if(d['condition']=="Partly sunny") {
  60.                             condWeather = '⛅';
  61.                         }
  62.                         else if(d['condition']=="Overcast") {
  63.                             condWeather = '☁';
  64.                         }
  65.                         else if(d['condition']=="Rain") {
  66.                             condWeather = '☂';
  67.                         }
  68.  
  69.                         let condition = $('<span class="upcoming">')
  70.                             .append($('<span class="symbol">').text(condWeather));
  71.                         let highLow = $('<span class="forecast-data">').text(`${d['low']}°/${d['high']}°`);
  72.                         let forecast = $('<span class="forecast-data">').text(`${d['condition']}`);
  73.                         condition.append(highLow);
  74.                         condition.append(forecast);
  75.                         $('#upcoming').append(condition)
  76.  
  77.                     }
  78.  
  79.  
  80.  
  81.  
  82.                 }).catch(displayError)
  83.  
  84.             }).catch(displayError);
  85.  
  86.         }).catch(displayError)
  87.     }
  88.  
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement