Oh, sure enough! I just tried it in Chrome and it refused the data. Normally CORS proxies are needed because of the browser’s same-origin policy (it will reject executable code from anywhere but the server it came from). In this case, DarkSky has disabled CORS from the server by not including an important header. Turns out, they don’t actually want people using an API key in client side JavaScript. I guess a CORS proxy is our little way of sticking it to “The Man”!
You’ve got a for
loop defined on line 96
. The reason your button isn’t working right now is because you’re defining both tempState
and #convertTemp
’s click handler on each iteration of this loop. In order for this to work, both of these need to be defined outside of the loop. I placed tempState
towards the top and the handler just before the loop’s definition and I was able to convert the current day’s temperature. Getting that to change all of the temperatures will take a bit of finagling. However, let’s talk about that loop for a second.
What’s interesting is that you’re quite close to getting this set up yourself. It looks like you had an idea to do it yourself, but didn’t. You’ve got that loop on line 96
, but inside the loop you’ve hard-coded all of the array access.
var temp02 = json.daily.data[1].apparentTemperatureMax;
A lot of your code is repeated just because you’ve hard coded these indices. Here’s what I would do in your situation:
for (i = 0; i < json.daily.data.length; i++) {
var temp = Math.round(json.daily.data[i].apparentTemperatureMax);
$('#temp' + i).html(temp + "°F");
var icon = json.daily.data[i].icon;
$("#icon" + i).html("<i class='wi wi-forecast-io-" + icon + "'></i>");
}
Change the ids you have in your markup and this should work dandy. This dovetails into your other loop starting on 36
. You could probably use the same loop as above, but again, you’d need to change the ids in your markup.
var date = moment().add(i, "day").format("ddd");
$('#day' + i).html(date);
Now, if this seems like a lot of work, that’s because it is. If you’re interested in learning a simple but awesome technique that makes this sort of app much, much easier, then take a look at Handlebars for templating. I have a very simple example on its use here. Take a gander - it’s 24 lines of JavaScript and it should show you everything you need to apply this to your own app. Don’t worry, it’s not a Free Code Camp project, so it’s not cheating to look.
So, changing your temperature for every day in the forecast will be tricky even with templating. Having the data pre-calculated isn’t going to work as well, so it’s kinda like the entirety of my last post was useless drivel
. What I would do is make sure every element that holds a temperature has a class in common (temp
would work well). Then, you can use jQuery to select all of those elements.
$('.temp').each(function(index, $element) { //<-- jQuery function
var currentTemp = $element.html()
if(tempState === 0) {
// convert currentTemp to Celcius and reassign to $element.html
} else {
// convert currentTemp to Farenheit and reassign to $element.html
})
Hope this post is more helpful!