How to return the value of a variable in this situation

How to return the value of a variable in this situation
0

#1

Hey, I’m working on “Show the Local Weather” task.
I’ve already defined all the variables but in this function, I cannot return the value of urlString.

if (navigator.geolocation) {
  navigator.geolocation.getCurrentPosition(showPosition);
} else {
  console.log("Geolocation is not supported by this browser");
}

function showPosition(position) {
  var lat = "lat=" + position.coords.latitude;
  var lon = "lon=" + position.coords.longitude;
  var urlString = api + lat + "&" + lon + "&appid=" + apikey;
  return urlString
}

console.log(showPosition);

How to return this value?
Thanks :slightly_smiling_face:


#2

You can’t return a value of a function using console.log.

You can immediately use your variable urlString in an ajax call, instead of returning it:

function showPosition(position) {
  var lat = "lat=" + position.coords.latitude;
  var lon = "lon=" + position.coords.longitude;
  var urlString = api + lat + "&" + lon + "&appid=" + apikey;
  
  /*Your ajax call goes here...*/
}