Show the Local Weather, please help me make it more functional?

Hey, I have just finished this project, with some huge help already, pointing me in the right direction. Much appreciated and I do understand certain aspects a lot better, but I feel im not there yet, enough to progress further. My codes works but I want to do things with it, things I dont know how, to make it better and more functional. Please help me make my temperature conversion into a function on its own, so I can call it. I know my code is a mess, and I really want to start in the right direction on functioning my code, making things easy to see and use.

//decalre some global variables
var api = "";
var lat;
var long;
var currentTemp;
var far;

$(document).ready(function() { //on document load
  if (navigator.geolocation) { //if geolocation allowed
    navigator.geolocation.getCurrentPosition(function(position) { 
      var lat = position.coords.latitude; //store lat position
      var long = position.coords.longitude; //store long position
      getInfo(lat, long); //call getInfo function
  } else {
    console.log("geolocation not supported"); //if not supported
//function to get all the data and use it on the screen
function getInfo(lat, long) { //pass lat and long thrugh function
  var urlstring = api + lat + "&&lon=" + long; //decalre url variable to get location
    url: urlstring, //use this url
    success: function(data) { //successfull
      var icon = '"' +[0].icon + '"'; //this is the built in icons on API
      var overView =[0].main; //overview from object
      var desc =[0].description; //decirption
      //needed to use 2 variables. One for rounded and one for working out far conversion. Kept getting NAN when trying to use currentTemp rounded with Far
      var currentTemp = data.main.temp;
      var currentTempRounded = Math.round(currentTemp) + String.fromCharCode(176) + "C";
      var far = currentTemp * (9 / 5) + 32;
      var farRounded = Math.round(far) + String.fromCharCode(176) + "F";
      var name =;
      //Put data in span locations....
      $("#icon").append("<image src=" + icon + "</image>");
      //I REALLY want to make this its own function!! Outside of getInfo();
      //litte on click function, if Celsius rounded change to far rounded
      //else switch to celsius
$("#temp").click(function() {
  if ($("#temp").text() == currentTempRounded){

WIll also attach my Codepen, its enough to pass the problem, but to me, could be so much better, not so much what it does, how im coding it.


I took your solution and created the following function:

function showTemp() {
   var currentTempRounded = Math.round(
     currentUnit === "C" ? celcius : celcius * (9 / 5) + 32
   $("#temp").text(currentTempRounded + String.fromCharCode(176) + currentUnit);
   currentUnit = currentUnit === "C" ? "F" : "C";

Then I created the following two global variables (declared at top of code):

var celcius;
var currentUnit = "C";

Since the temperature you get back from the API is already in celcius, then in the success function you can have:

celcius = data.main.temp;

then a few lines later when you are populating the various data elements, you would call the showTemp function.

Finally, the temperature click event is simple, because you can just put the following line at the bottom within your $(document).ready(function() and showTemp will take care of all temperature changes.


You can probably take what I have done and streamline further if you want.

Most of your commenting is redundant. The only comments that potentially add clarity are:

//function to get all the data and use it on the screen
//this is the built in icons on API
//needed to use 2 variables. One for rounded and one for working out far conversion. Kept getting NAN when trying to use currentTemp rounded with Far

Even then, the first of those would be better as a more descriptive function name (getAndPrintInfo() or similar). The fact that there’s an “and” in that name also suggests it should probably be two functions, getInfo() and populateUI() or something. Then, in your document ready handler, you could have something like the following:

var weatherInfo = getInfo(lat, long); //getInfo returns an object with all the data you need
populateUI(weatherInfo); //populateUI uses the data from the returned object

In fact, looking at things like commenting and naming can often reveal ways to further modularize your code like this.

Regarding when you should comment, this is a good article:

Thanks for the feedback, I will make some improvements!