Hello All.
I am working on the Local weather app, which have taken me a insane amount of time. I am able to see light in the tunnel now, but have a issue I don`t seem to be able to solve when trying to set icons to the weekly forecast.
I could solve this the “easy” way by just write big amounts of repeating code, but would very much like to solve this in a more elegant way (and understand it at the same time )
So at the bottom of this code I have a function called setWeekIcon where i have a for loop which function is to loop over an array of weather icons. Then I try to pass in arguments from a iconArray, which again holds the weather forecast icons from the Dark Sky API.
I tried to use a IF ELSE condition, but understood that this is not the way to go, as I then only will be able to fulfill the two first conditions. Since then I have been trying to search the web for inspiration, but have not been able to figure it out.
Appreciate it if anybody have some advice and I apologize if I don`t make any sense.
BR
Tommy
//Global variables
var units = ‘si’;
var lat;
var lon;
var temp;
//var icon = " “;
var resp;
var dailyTemp;
var weekday = [“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”]; //to find day of the week
var dayArray = ; //to store days of the week
var iconArray = ; //to store icon values
var tempArray = ; //to store temps
//Get weather data
function getWeather(longitude, latitude) {
$.getJSON(‘How Dark Sky users can use the Apple Weather app - Apple Support’ + latitude + ‘,’ + longitude + ‘?callback=?&units=’ + units + ‘’, function (json) {
temp = json.currently.temperature;
var icon = json.currently.icon;
console.log(json);
$(”#temp").html(Math.round(temp) + (‘°C’));
$(“#desc”).html(json.currently.summary);
$(“#forecast”).html(json.daily.summary);
var weatherIcon = json.daily.data[i].icon;
iconArray.push(weatherIcon);
var dailyTemp = json.daily.data[i].apparentTemperatureMax;
tempArray.push(dailyTemp);var imageArray = [ { "src": "icons/b100/09.png" , "title": "rain" , "imageCaption": "Image caption for rain" } , { "src": "icons/b100/mf/01n.50.png" , "title": "clear-night" , "imageCaption": "Image caption for clear night" } , { "src": "icons/b100/01d.png" , "title": "clear-day" , "imageCaption": "Image caption for clear day" } , { "src": "icons/b100/13.png" , "title": "snow" , "imageCaption": "Image caption for snow" } , { "src": "icons/b100/12.png" , "title": "sleet" , "imageCaption": "Image caption for sleet" } , { "src": "icons/b100/15.png" , "title": "fog" , "imageCaption": "Image caption for fog" } , { "src": "icons/b100/03d.png" , "title": "partly-cloudy-day" , "imageCaption": "Image caption for partly cloudy day" } , { "src": "icons/b100/01n.png" , "title": "clear-night" , "imageCaption": "Image caption for partly clear night" } , { "src": "icons/b100/mf/03n.50.png" , "title": "partly-cloudy-night" , "imageCaption": "Image caption for partly partly cloudy night" } ]; function setWeekIcon(title1,title2,title3,title4,title5,title6) { for (var i = 0; i < imageArray.length; i++) { if (imageArray[i].title == title1) { $('#icon2').attr('src', imageArray[i].src); }else if (imageArray[i].title == title2) { $('#icon3').attr('src', imageArray[i].src); }else if (imageArray[i].title == title3) { $('#icon4').attr('src', imageArray[i].src); }else if (imageArray[i].title == title4) { $('#icon5').attr('src', imageArray[i].src); }else if (imageArray[i].title == title5) { $('#icon6').attr('src', imageArray[i].src); }else if (imageArray[i].title == title6) { $('#icon7').attr('src', imageArray[i].src); } } return "Error"; } setWeekIcon(iconArray[0],iconArray[1],iconArray[2],iconArray[3],iconArray[4],iconArray[5]);
});
};