const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var xmlhttp = new XMLHttpRequest();

var latitude = 48.148641;
var longitude = 11.568721;

console.log(getPopulation(latLonToCity(latitude, longitude)));

function latLonToCity(latitude, longitude) {
    var url = 'http://nominatim.openstreetmap.org/reverse?format=json'
                + '&accept-language=en-US'
                + '&lat=' + latitude + '&lon=' + longitude;

    var city;

    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var data = JSON.parse(this.responseText);
            city = getCity(data);
        }
    };
    xmlhttp.open("GET", url, false);
    xmlhttp.send();

    return city;
}

function getCity(data) {
    if (data['address']['city'] !== undefined) {
        return data['address']['city'];
    }
    if (data['address']['town'] !== undefined) {
        return data['address']['town'];
    }
    if (data['address']['village'] !== undefined) {
        return data['address']['village'];
    }
    return 'failure\n' + data;
}

function getPopulation(place) {
    var url = 'https://en.wikipedia.org/wiki/' + place;

    var population;
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            population = extractNumber(this.responseText);
        }
    };
    xmlhttp.open("GET", url, false);
    xmlhttp.send();

    return population;
}

function extractNumber(str) {
    var offset = str.search('City</th>\n<td>');
    console.log(offset);
    for (var i = 0; i < 25; i++) {
        console.log(str[offset+i]);
    }
    var input = '';
    var spaces = 0;
    var i = 0;
    while (spaces < 2) {
        if (str[offset+13+i] === ' ') {
            spaces++;
        }
        input += str[offset+13+i];
        i++;
    }
    var population_string = '';
    i = 0;
    while (input[i] !== ' ') {
        population_string += input[i];
        i++;
    }
    var population = parseFloat(population_string);
    if (input.indexOf('million') > -1) {
        population = population * 1000000;
    }
    return population;
}

@@ -1544,106 +1544,53 @@
     
-console.log(offset);%0A    for (var i = 0; i %3C 25; i++) %7B%0A        console.log(str%5Boffset+i%5D
+var splitted = str.split('City%3C/th%3E%5Cn%3Ctd%3E'
 );%0A
-    %7D%0A
     
@@ -1598,17 +1598,12 @@
 ar i
-nput
  = 
-''
+0
 ;%0A  
@@ -1612,33 +1612,26 @@
 var 
-spaces = 0;%0A    var i = 0
+number_string = ''
 ;%0A  
@@ -1645,55 +1645,27 @@
  (sp
-aces %3C 2) %7B%0A        if (str%5Boffset+13+
+litted%5B2%5D%5B
 i%5D 
+!
 ==
-=
  '
- 
+%3C
 ') %7B
@@ -1677,359 +1677,139 @@
     
-    spaces++;%0A        %7D%0A        input += str%5Boffset+13+i%5D;%0A        i++;%0A    %7D%0A    var population_string = '';%0A    i = 0;%0A    while (input%5Bi%5D !== ' ') %7B%0A        population_string += input%5Bi%5D;%0A        i++;%0A    %7D%0A    var population = parseFloat(population_string);%0A    if (input.indexOf('million') %3E -1) %7B%0A        population = population * 1000000;%0A    %7D
+number_string += splitted%5B2%5D%5Bi%5D;%0A        i++;%0A    %7D%0A    console.log(number_string);%0A    var population = parseFloat(number_string);
 %0A   

return the population but as string (by now)