Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function attachEvents() {
- let baseURL='https://judgetests.firebaseio.com/locations.json';
- let userInput='';
- $('#submit').click(function () {
- userInput=$('#location').val();
- $.get(baseURL)
- .then(codeValue)
- .catch(weatherError);
- });
- function codeValue(data) {
- let townCode='';
- for (let obj of data) {
- let code=obj.code;
- let name=obj.name;
- if(userInput===name){
- townCode=code;
- break;
- }
- }
- let currentConditionsUrl=`https://judgetests.firebaseio.com/forecast/today/${townCode}.json`;
- $.get(currentConditionsUrl).then(currentConditions);
- function currentConditions(data) {
- $('#current').empty();
- let current=$('#current');
- $('#forecast').css('display','inline-block');
- // console.log(data.forecast.condition,data.forecast.high,data.forecast.low);
- let label=$('<div>').attr('class','label').text('Current conditions');
- current.append(label);
- let icon=getIcon(data.forecast.condition);
- let span=$('<span>').attr('class','condition symbol').html(icon);
- current.append(span);
- let conditions=$('<span>').attr('class','condition');
- let span1=$('<span>').attr('class','forecast-data').text(data.name);
- conditions.append(span1);
- let span2=$('<span>').attr('class','forecast-data').html(data.forecast.low+'\°/'+data.forecast.high+'\°');
- conditions.append(span2);
- let span3=$('<span>').attr('class','forecast-data').text(data.forecast.condition);
- conditions.append(span3);
- current.append(conditions);
- } //end of currentConditions
- let upcomingConditionsUrl=`https://judgetests.firebaseio.com/forecast/upcoming/${townCode}.json`;
- $.get(upcomingConditionsUrl).then(upcomingConditions);
- function upcomingConditions(data) {
- $('.upcoming').detach();
- let upcoming=$('#upcoming');
- for (let obj of data.forecast) {
- // console.log(obj.condition,obj.log,obj.high);
- let icon=getIcon(obj.condition);
- let span=$('<span>').attr('class','upcoming');
- upcoming.append(span);
- let line= $('<span>').attr('class','symbol').html(icon);
- span.append(line);
- let span1=$('<span>').attr('class','forecast-data').html(obj.low+'\°/'+obj.high+'\°');
- span.append(span1);
- let span2=$('<span>').attr('class','forecast-data').html(obj.condition);
- span.append(span2);
- }
- }
- function getIcon(x){
- switch(x){
- case 'Sunny':
- return '\☀';
- break;
- case 'Partly sunny':
- return '\⛅';
- break;
- case 'Rain':
- return '\☔';
- break;
- case 'Overcast':
- return '\☁';
- break;
- case 'Degrees':
- return '\°';
- break;
- default:
- break;
- }
- }
- } //end of codeValue
- function weatherError() {
- $('#forecast').text('Error');
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment