Uncaught TypeError: Cannot read property 'indexOf' of undefined function get continent google map

Sorry if question is already answered but i can’t resolve problem

i have elaborated a piece of code to get continent where country is located in google map:

<!doctype html>
<html>
  <head>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.7.1.js">
    </script>
    <script>
      var map;
      var marker;
      var geocoder;
      // this will be the service that searches an address and returns the position
      function initMap() {
        var initialCenter = new google.maps.LatLng(50.5, 4.50);
        // somewhere in Belgium
        map = new google.maps.Map(document.getElementById('map'), {
          zoom: 8,
          center: initialCenter,
          mapTypeId: 'terrain'
        }
                                 );
        geocoder = new google.maps.Geocoder();
      }
      // search their location
      function searchAddress() {
        var addressString = document.getElementById('search').value;
        addressToPosition(addressString);
      }
      // draggable marker
      function placeMarker(lat, lng) {
        // if a previous marker exists, let's first remove it from the map
        if(marker) {
          marker.setMap(null);
        }
        // place the marker
        marker = new google.maps.Marker({
          position: new google.maps.LatLng(lat, lng),
          map: map,
          draggable: true
        }
                                       );
        // on drag-end, do this:
        marker.addListener('dragend', function(e) {
          var newPosition = marker.getPosition();
          var lat = Number(newPosition.lat());
          var lng = Number(newPosition.lng());
          putCoordinates(lat, lng);
          // search address by lat, lng
          addressToPosition(lat +','+ lng);
        }
                          );
      }
      // put the gps coordinates in input elements
      function putCoordinates(lat, lng) {
        document.getElementById('lat').value = lat;
        document.getElementById('lng').value = lng;
      }
      // search for an address
      function addressToPosition(addressString) {
        geocoder.geocode( {
          'address': addressString}
                         , function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            if (status != google.maps.GeocoderStatus.ZERO_RESULTS) {
              var position = results[0].geometry.location;
              var lat = position.lat();
              var lng = position.lng();
              // center the map
              map.setCenter(position);
              // zoom a little
              if(map.getZoom() < 10) {
                map.setZoom(14);
              }
              // place marker
              placeMarker(lat, lng);
              // display coordinates in input
              putCoordinates(lat, lng);
              // clear previous values
              clearElementsByClass('component');
              // address components
              for(var i in results[0].address_components) {
                // search for postal code
                if(results[0].address_components[i].types.indexOf('postal_code') > -1) {
                  document.getElementById('postal_code').value = results[0].address_components[i].long_name;
                }
                // search for sublocality
                if(results[0].address_components[i].types.indexOf('sublocality') > -1) {
                  document.getElementById('sublocality').value = results[0].address_components[i].long_name;
                }
                // search for locality
                if(results[0].address_components[i].types.indexOf('locality') > -1) {
                  document.getElementById('locality').value = results[0].address_components[i].long_name;
                }
                // search for administrative_area_level_1
                if(results[0].address_components[i].types.indexOf('administrative_area_level_1') > -1) {
                  document.getElementById('administrative_area_level_1').value = results[0].address_components[i].long_name;
                }
                // search for route
                if(results[0].address_components[i].types.indexOf('route') > -1) {
                  document.getElementById('route').value = results[0].address_components[i].long_name;
                }
                // search for street_number
                if(results[0].address_components[i].types.indexOf('street_number') > -1) {
                  document.getElementById('street_number').value = results[0].address_components[i].long_name;
                }
                // search for country
                if(results[0].address_components[i].types.indexOf('country') > -1) {
                  document.getElementById('country').value = results[0].address_components[i].long_name;
                }
                // search for continent
                if (results[0].address_components[i].types.indexOf('country','political') > -1)
                {
                  document.getElementById('codecountry').value = results[0].address_components[i].short_name;
                  codecountrycode = results[0].address_components[i].short_name;
                  $(function convertcodetocontinent() {
                    var Codes = function(code, name) {
                      this.code = code;
                      this.name = name;
                    };
                    var codecontinent = [
                      new Codes("AD", "Europe"),
                      new Codes("AE", "Asia"),
                      new Codes("AF", "Asia"),
                      new Codes("AG", "North America"),
                      new Codes("AI", "North America"),
                      new Codes("AL", "Europe"),
                      new Codes("AM", "Asia"),
                      new Codes("AN", "North America"),
                      new Codes("AO", "Africa"),
                      new Codes("AQ", "Antarctica"),
                      new Codes("AR", "South America"),
                      new Codes("AS", "Australia"),
                      new Codes("AT", "Europe"),
                      new Codes("AU", "Australia"),
                      new Codes("AW", "North America"),
                      new Codes("AZ", "Asia"),
                      new Codes("BA", "Europe"),
                      new Codes("BB", "North America"),
                      new Codes("BD", "Asia"),
                      new Codes("BE", "Europe"),
                      new Codes("BF", "Africa"),
                      new Codes("BG", "Europe"),
                      new Codes("BH", "Asia"),
                      new Codes("BI", "Africa"),
                      new Codes("BJ", "Africa"),
                      new Codes("BM", "North America"),
                      new Codes("BN", "Asia"),
                      new Codes("BO", "South America"),
                      new Codes("BR", "South America"),
                      new Codes("BS", "North America"),
                      new Codes("BT", "Asia"),
                      new Codes("BW", "Africa"),
                      new Codes("BY", "Europe"),
                      new Codes("BZ", "North America"),
                      new Codes("CA", "North America"),
                      new Codes("CC", "Asia"),
                      new Codes("CD", "Africa"),
                      new Codes("CF", "Africa"),
                      new Codes("CG", "Africa"),
                      new Codes("CH", "Europe"),
                      new Codes("CI", "Africa"),
                      new Codes("CK", "Australia"),
                      new Codes("CL", "South America"),
                      new Codes("CM", "Africa"),
                      new Codes("CN", "Asia"),
                      new Codes("CO", "South America"),
                      new Codes("CR", "North America"),
                      new Codes("CU", "North America"),
                      new Codes("CV", "Africa"),
                      new Codes("CX", "Asia"),
                      new Codes("CY", "Asia"),
                      new Codes("CZ", "Europe"),
                      new Codes("DE", "Europe"),
                      new Codes("DJ", "Africa"),
                      new Codes("DK", "Europe"),
                      new Codes("DM", "North America"),
                      new Codes("DO", "North America"),
                      new Codes("DZ", "Africa"),
                      new Codes("EC", "South America"),
                      new Codes("EE", "Europe"),
                      new Codes("EG", "Africa"),
                      new Codes("EH", "Africa"),
                      new Codes("ER", "Africa"),
                      new Codes("ES", "Europe"),
                      new Codes("ET", "Africa"),
                      new Codes("FI", "Europe"),
                      new Codes("FJ", "Australia"),
                      new Codes("FK", "South America"),
                      new Codes("FM", "Australia"),
                      new Codes("FO", "Europe"),
                      new Codes("FR", "Europe"),
                      new Codes("GA", "Africa"),
                      new Codes("GB", "Europe"),
                      new Codes("GD", "North America"),
                      new Codes("GE", "Asia"),
                      new Codes("GF", "South America"),
                      new Codes("GG", "Europe"),
                      new Codes("GH", "Africa"),
                      new Codes("GI", "Europe"),
                      new Codes("GL", "North America"),
                      new Codes("GM", "Africa"),
                      new Codes("GN", "Africa"),
                      new Codes("GP", "North America"),
                      new Codes("GQ", "Africa"),
                      new Codes("GR", "Europe"),
                      new Codes("GS", "Antarctica"),
                      new Codes("GT", "North America"),
                      new Codes("GU", "Australia"),
                      new Codes("GW", "Africa"),
                      new Codes("GY", "South America"),
                      new Codes("HK", "Asia"),
                      new Codes("HN", "North America"),
                      new Codes("HR", "Europe"),
                      new Codes("HT", "North America"),
                      new Codes("HU", "Europe"),
                      new Codes("ID", "Asia"),
                      new Codes("IE", "Europe"),
                      new Codes("IL", "Asia"),
                      new Codes("IM", "Europe"),
                      new Codes("IN", "Asia"),
                      new Codes("IO", "Asia"),
                      new Codes("IQ", "Asia"),
                      new Codes("IR", "Asia"),
                      new Codes("IS", "Europe"),
                      new Codes("IT", "Europe"),
                      new Codes("JE", "Europe"),
                      new Codes("JM", "North America"),
                      new Codes("JO", "Asia"),
                      new Codes("JP", "Asia"),
                      new Codes("KE", "Africa"),
                      new Codes("KG", "Asia"),
                      new Codes("KH", "Asia"),
                      new Codes("KI", "Australia"),
                      new Codes("KM", "Africa"),
                      new Codes("KN", "North America"),
                      new Codes("KP", "Asia"),
                      new Codes("KR", "Asia"),
                      new Codes("KW", "Asia"),
                      new Codes("KY", "North America"),
                      new Codes("KZ", "Asia"),
                      new Codes("LA", "Asia"),
                      new Codes("LB", "Asia"),
                      new Codes("LC", "North America"),
                      new Codes("LI", "Europe"),
                      new Codes("LK", "Asia"),
                      new Codes("LR", "Africa"),
                      new Codes("LS", "Africa"),
                      new Codes("LT", "Europe"),
                      new Codes("LU", "Europe"),
                      new Codes("LV", "Europe"),
                      new Codes("LY", "Africa"),
                      new Codes("MA", "Africa"),
                      new Codes("MC", "Europe"),
                      new Codes("MD", "Europe"),
                      new Codes("ME", "Europe"),
                      new Codes("MG", "Africa"),
                      new Codes("MH", "Australia"),
                      new Codes("MK", "Europe"),
                      new Codes("ML", "Africa"),
                      new Codes("MM", "Asia"),
                      new Codes("MN", "Asia"),
                      new Codes("MO", "Asia"),
                      new Codes("MP", "Australia"),
                      new Codes("MQ", "North America"),
                      new Codes("MR", "Africa"),
                      new Codes("MS", "North America"),
                      new Codes("MT", "Europe"),
                      new Codes("MU", "Africa"),
                      new Codes("MV", "Asia"),
                      new Codes("MW", "Africa"),
                      new Codes("MX", "North America"),
                      new Codes("MY", "Asia"),
                      new Codes("MZ", "Africa"),
                      new Codes("NA", "Africa"),
                      new Codes("NC", "Australia"),
                      new Codes("NE", "Africa"),
                      new Codes("NF", "Australia"),
                      new Codes("NG", "Africa"),
                      new Codes("NI", "North America"),
                      new Codes("NL", "Europe"),
                      new Codes("NO", "Europe"),
                      new Codes("NP", "Asia"),
                      new Codes("NR", "Australia"),
                      new Codes("NZ", "Australia"),
                      new Codes("OM", "Asia"),
                      new Codes("PA", "North America"),
                      new Codes("PE", "South America"),
                      new Codes("PF", "Australia"),
                      new Codes("PG", "Australia"),
                      new Codes("PH", "Asia"),
                      new Codes("PK", "Asia"),
                      new Codes("PL", "Europe"),
                      new Codes("PM", "North America"),
                      new Codes("PN", "Australia"),
                      new Codes("PR", "North America"),
                      new Codes("PS", "Asia"),
                      new Codes("PT", "Europe"),
                      new Codes("PW", "Australia"),
                      new Codes("PY", "South America"),
                      new Codes("QA", "Asia"),
                      new Codes("RE", "Africa"),
                      new Codes("RO", "Europe"),
                      new Codes("RS", "Europe"),
                      new Codes("RU", "Europe"),
                      new Codes("RW", "Africa"),
                      new Codes("SA", "Asia"),
                      new Codes("SB", "Australia"),
                      new Codes("SC", "Africa"),
                      new Codes("SD", "Africa"),
                      new Codes("SE", "Europe"),
                      new Codes("SG", "Asia"),
                      new Codes("SH", "Africa"),
                      new Codes("SI", "Europe"),
                      new Codes("SJ", "Europe"),
                      new Codes("SK", "Europe"),
                      new Codes("SL", "Africa"),
                      new Codes("SM", "Europe"),
                      new Codes("SN", "Africa"),
                      new Codes("SO", "Africa"),
                      new Codes("SR", "South America"),
                      new Codes("ST", "Africa"),
                      new Codes("SV", "North America"),
                      new Codes("SY", "Asia"),
                      new Codes("SZ", "Africa"),
                      new Codes("TC", "North America"),
                      new Codes("TD", "Africa"),
                      new Codes("TF", "Antarctica"),
                      new Codes("TG", "Africa"),
                      new Codes("TH", "Asia"),
                      new Codes("TJ", "Asia"),
                      new Codes("TK", "Australia"),
                      new Codes("TM", "Asia"),
                      new Codes("TN", "Africa"),
                      new Codes("TO", "Australia"),
                      new Codes("TR", "Asia"),
                      new Codes("TT", "North America"),
                      new Codes("TV", "Australia"),
                      new Codes("TW", "Asia"),
                      new Codes("TZ", "Africa"),
                      new Codes("UA", "Europe"),
                      new Codes("UG", "Africa"),
                      new Codes("US", "North America"),
                      new Codes("UY", "South America"),
                      new Codes("UZ", "Asia"),
                      new Codes("VC", "North America"),
                      new Codes("VE", "South America"),
                      new Codes("VG", "North America"),
                      new Codes("VI", "North America"),
                      new Codes("VN", "Asia"),
                      new Codes("VU", "Australia"),
                      new Codes("WF", "Australia"),
                      new Codes("WS", "Australia"),
                      new Codes("YE", "Asia"),
                      new Codes("YT", "Africa"),
                      new Codes("ZA", "Africa"),
                      new Codes("ZM", "Africa"),
                      new Codes("ZW", "Africa")
                    ];
                    var utils = {
                    };
                    // Could create a utility function to do this
                    utils.inArray = function(searchFor, property) {
                      var retVal = -1;
                      var self = this;
                      for (var index = 0; index < self.length; index++) {
                        var item = self[index];
                        if (item.hasOwnProperty(property)) {
                          if (item[property].toLowerCase() === searchFor.toLowerCase()) {
                            retVal = index;
                            return retVal;
                          }
                        }
                      };
                      return retVal;
                    };
                    // or we could create a function on the Array prototype indirectly
                    Array.prototype.inArray = utils.inArray;
                    // let's use the prototype for now
                    var i = codecontinent.inArray(codecountrycode, "code");
                    //$('#output').text(codecontinent[i].name);
                    $(":input[id=continent]").val(codecontinent[i].name);
                    // or we could create a function on the Array prototype directly
                    /*
        Array.prototype.inArray2 = function(searchFor, property) {
                var retVal = -1;
                $.each(this, function(index, item) {
                        if (item.hasOwnProperty(property)) {
                                if (item[property].toLowerCase() === searchFor.toLowerCase()) {
                                        retVal = index;
                                        return false;
                                }
                        }
                });
                return retVal;
        };
        */
                  }
                   );
                }
              }
            }
          }
        }
                        );
      }
      //
      function clearElementsByClass(className) {
        var elements = document.getElementsByClassName(className);
        for(var i in elements) {
          elements[i].value="";
        }
      }
      ///////////
      // track client position
      function trackPosition() {
        if(navigator.geolocation) {
          navigator.geolocation.getCurrentPosition(function(position) {
            var lat = position.coords.latitude;
            var lng = position.coords.longitude;
            // place marker
            placeMarker(lat, lng);
            // display coordinates in input
            putCoordinates(lat, lng);
            // search address by lat, lng
            addressToPosition(lat +','+ lng);
          }
                                                   ,
                                                   function(position) {
          }
                                                  )
        }
      }
    </script>
    <style>
      #map {
        width: 700px;
        height: 450px;
      }
    </style>
  </head>
  <body>
    <div id="map">
    </div>
    <input id="search"/>
    <input type="button" value="SEARCH" onclick="searchAddress()" />
    <br/>
    <input type="button" value="TRACK" onclick="trackPosition()" />
    <hr/>
    <form action="post.php" method="post">
      <input class="position" id="lat" name="lat" placeholder="latitude"/>
      <input class="position" id="lng" name="lng" placeholder="longitude"/>
      <input class="component" id="route" name="route" placeholder="route"/>
      <input class="component" id="street_number" name="street_number" placeholder="street_number"/>
      <input class="component" id="sublocality" name="sublocality" placeholder="sublocality"/>
      <input class="component" id="locality" name="locality" placeholder="locality"/>
      <input class="component" id="administrative_area_level_1" name="administrative_area_level_1" placeholder="administrative_area_level_1"/>
      <input class="component" id="postal_code" name="postal_code" placeholder="postal_code"/>
      <input class="component" id="country" name="country" placeholder="country"/>
      <input class="component" id="continent" name="continent" placeholder="continent" />
      <input class="component" id="codecountry" name="codecountry" placeholder="codecountry" />
      <input type="submit" value="Submit data"/>
    </form>
    <script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCUYsLGs_ek6Ids4TN1ZZeJvv6X-r4j5N4&callback=initMap">
    </script>
  </body>
</html>

the code work ( i get all data included continent) but in Google Chrome console i get following error:

Uncaught TypeError: Cannot read property 'indexOf' of undefined(anonymous function) @ prev6~.php:81qT @ geocoder.js:5(anonymous function) @ geocoder.js:3hT @ geocoder.js:2(anonymous function) @ geocoder.js:3(anonymous function) @ common.js:52Uj @ common.js:11_.Zm @ common.js:52c @ common.js:46(anonymous function) @ GeocodeService.Search?4snew york&7sUS&9sen-US&key=AIzaSyCUYsLGs_ek6Ids4TN1ZZeJvv6X-r4j5N4&callback=…:1

Is there something wrong with the code?

Thanks for your help.

One obvious thing you can do, is skip any iteration without types:

for(var i in results[0].address_components) {
  if(typeof results[0].address_components[i].types == 'undefined') {
    continue;
  }
  if(results[0].address_components[i].types.indexOf('postal_code') > -1) {
  ...