Advertisement
nikolayneykov

Untitled

Jul 14th, 2019
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.   let weatherCodes = {
  3.     Sunny: '☀',
  4.     'Partly sunny': '⛅',
  5.     Overcast: '☁',
  6.     Rain: '☂'
  7.   };
  8.  
  9.   document.querySelector('#submit').addEventListener('click', catchTheWeather);
  10.   document.querySelector('#forecast').style.display = 'block';
  11.  
  12.   function printError() {
  13.     let divForecasts = document.createElement('span');
  14.     divForecasts.setAttribute('class', 'forecasts');
  15.     divForecasts.textContent = 'Error!';
  16.     document.querySelector('#current').appendChild(divForecasts);
  17.   }
  18.  
  19.   async function catchTheWeather() {
  20.     let location = document.querySelector('#location').value;
  21.     location = location.toLowerCase();
  22.     if (
  23.       location === 'london' ||
  24.       location === 'new york' ||
  25.       location === 'barcelona'
  26.     ) {
  27.       const response = await fetch(
  28.         `https://judgetests.firebaseio.com/forecast/today/${location}.json `
  29.       );
  30.       const data = await response.json();
  31.       let weatherType = data.forecast.condition;
  32.       let currentLocation = data.name;
  33.       let degreece = `${data.forecast.low}°/${data.forecast.high}°`;
  34.       createCurrentCondition(weatherType, currentLocation, degreece);
  35.     } else {
  36.       printError();
  37.     }
  38.   }
  39.  
  40.   function createCurrentCondition(weatherType, location, degreece) {
  41.     let divForecasts = document.createElement('span');
  42.     divForecasts.setAttribute('class', 'forecasts');
  43.     let spanConditionSymb = document.createElement('span');
  44.     spanConditionSymb.setAttribute('class', 'condition symbol');
  45.     spanConditionSymb.textContent = weatherCodes[weatherType];
  46.     let spanCondition = document.createElement('span');
  47.     spanCondition.setAttribute('class', 'condition');
  48.  
  49.     let span1 = document.createElement('span');
  50.     let span2 = document.createElement('span');
  51.     let span3 = document.createElement('span');
  52.     span1.setAttribute('class', 'forecast-data');
  53.     span2.setAttribute('class', 'forecast-data');
  54.     span3.setAttribute('class', 'forecast-data');
  55.     span1.textContent = location;
  56.     span2.textContent = degreece;
  57.     span3.textContent = weatherType;
  58.     spanCondition.appendChild(span1);
  59.     spanCondition.appendChild(span2);
  60.     spanCondition.appendChild(span3);
  61.     divForecasts.appendChild(spanConditionSymb);
  62.     divForecasts.appendChild(spanCondition);
  63.     document.querySelector('#current').appendChild(divForecasts);
  64.   }
  65.  
  66.   function createUpCommingCondition() {}
  67. }
  68.  
  69. attachEvents();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement