I'm building a weather app with Openweathermap API. The app already work but i have problem with temperature converter that still doesn't work.
Here is my HTML:
<div class='valign col s6 text-style'>
<div id="iconTemp">
<div id="icon"></div>
<div id="temp"></div>
</div>
<div class='divider'></div><br>
<div class="text-style-details">
<div class="location"></div>
<div id="conditions"></div>
<div id="wind"></div>
</div>
</div>
And my javascript:
<script type="text/javascript">
function ToC() {
var strIn = parseInt(temperature);
if(isNaN(strIn)) {
alert("Not a Number");
} else {
var f = parseFloat(strIn);
var c = (f - 32) * 5/9;
var r = Math.round(c * 100)/100;
parseInt(temperature) = r.toString();
}
}
function ToF() {
var strIn = parseFloat((temperature).toFixed(1));
if(isNaN(strIn)) {
alert("Not a Number");
} else {
var c = parseFloat(strIn);
var f = (c * 9/5) + 32;
var r = Math.round(f * 100)/100;
parseFloat((temperature).toFixed(1)) = r.toString();
}
}
$(document).ready(function() {
getLocationData();
function getLocationData() {
$.get("http://ipinfo.io", function(location) {
console.log(location);
$('.location')
.append(location.city + ", ")
.append(location.region);
var units = getUnits(location.country);
getWeatherData(location.loc, units);
}, "jsonp");
}
function getWeatherData(loc, units) {
lat = loc.split(",")[0]
lon = loc.split(",")[1]
var appid = "&APPID=123456abcde"
var url = 'http://api.openweathermap.org/data/2.5/weather?lat=' + lat + '&lon=' + lon + "&units=" + units + appid;
console.log(url);
$.get(url, function(weather) {
var windDir = WindDirection(weather.wind.deg);
var temperature = weather.main.temp;
var unitLabel;
if (units === "imperial") {
unitLabel = "<a href='#'>F</a>/<a href='#' onclick='ToC()'>C</a>";
// temperature = parseFloat((temperature).toFixed(1));
} else {
unitLabel = "<a href='#' onclick='ToF()'>C</a>";
// temperature = parseInt(temperature);
}
// temperature;
// temperature = parseInt(temperature);
// temperature = parseFloat((temperature).toFixed(1));
console.log(weather);
$('#icon')
// .append("<img src='http://openweathermap.org/img/w/" + weather.weather[0].icon + ".png'>");
.append("<i class='wi wi-owm-"+weather.weather[0].id+"'></i>");
// <i class="wi wi-owm-200"></i>
$('#temp').append(temperature + "° " + unitLabel);
$('#conditions').append(weather.weather[0].description);
$('#wind').append(windDir + " " + weather.wind.speed + " knots");
}, "jsonp");
};
function WindDirection(dir) {
var rose = ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'];
var eightPoint = Math.floor(dir / 45);
return rose[eightPoint];
}
function getUnits(country) {
var imperialCountries = ['US', 'ID', 'SG', 'MY', 'BS', 'BZ', 'KY', 'PW'];
if (imperialCountries.indexOf(country) === -1) {
var units = 'metric';
} else {
units = 'imperial';
}
console.log(country, units);
return units;
}
});
</script>
The app pict:
I created function ToC (convert F to C) and ToF ( convert C to F), when i click the label i get an error message "Uncaught ReferenceError: ToC is not defined". I want the app temperature value will be change when i click C (celcius) and F (fahrenheit).
Any help would be appreciated greatly, thank you.