Vijayabalaji
Vijayabalaji

Reputation: 41

Set 3 values to default values if only one of them returns 'undefined'

I am unable to set all three values (state, city, zip) to default (Sitka, Alaska, 99801) if only one of them returns 'undefined'.

We are using this code in Unbounce page builder with MaxMind geo location finder.

$(window).on('load', function() {
  $.getScript('//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js', function() {
    geoip2.city(function(geoipResponse) {
      let city = geoipResponse.city.names.en || 'Sitka'; // Default city to 'Sitka' if undefined
      let state = geoipResponse.subdivisions[0].names.en || 'Alaska'; // Default state to 'Alaska' if undefined
      let zip = geoipResponse.postal.code || '99801'; // Default zip to '99801' if undefined
      $('#lp-pom-form-520 #State').val(state);
      $('#lp-pom-form-520 #City').val(city);
      $('#lp-pom-form-520 #Zip').val(zip);
    }, function(error) {
      console.error('Error fetching geoip data:', error);
    });
  });
});

I also tried this, but it doesnt worked:

<script>
$(window).on('load', function() {
 $.getScript('//js.maxmind.com/js/apis/geoip2/v2.1/geoip2.js', function() {
  geoip2.city(function(geoipResponse) {
   let city = geoipResponse.city.names.en || 'Sitka';
   let state = geoipResponse.subdivisions[0].names.en || 'Alaska';
   let zip = geoipResponse.postal.code || '99801';
   // Set default values if any of the fields is undefined
   if (!city || !state || !zip) {
     city = 'Sitka';
     state = 'Alaska';
     zip = '99801';
   }
   $('#lp-pom-form-520 #State').val(state);
   $('#lp-pom-form-520 #City').val(city);
   $('#lp-pom-form-520 #Zip').val(zip);
  }, function(error) {
   console.error('Error fetching geoip data:', error);
  });
 });
});
</script>

Upvotes: 0

Views: 80

Answers (2)

passingThru
passingThru

Reputation: 1

It's too complex to use one-line logic (too many permutations) so I'd use a counter instead:

let U = 0; // the counter

if (typeof state === 'undefined') {
  U++;
}

if (typeof city === 'undefined') {
  U++;
}

if (typeof zip === 'undefined') {
  U++;
}

if (U == 1) {
  //If the criteria is only one must be undefined

  city = 'Sitka';
  state = 'Alaska';
  zip = '99801';
} else {
}

If I got your criteria wrong then you should make it clearer.

If anyone undefined should trigger default values, then use one-line logic...

if (
  typeof state === 'undefined' ||
  typeof city === 'undefined' ||
  typeof zip === 'undefined'
) {
  city = 'Sitka';
  state = 'Alaska';
  zip = '99801';
} else {
}

Btw, what you've got there doesn't test undefined but a logical NOT...

if (!city || !state || !zip) {}

Your test is only useful for boolean values and is equivalent to saying...

if (city === false || state === false || zip === false) {
}

You won’t be able to catch undefined values with an exclamation mark.

Upvotes: -2

VilgotanL
VilgotanL

Reputation: 410

In your second attempt, you forgot to remove the default values when declaring the variables, this is what it should be:

let city = geoipResponse.city.names.en;
let state = geoipResponse.subdivisions[0].names.en;
let zip = geoipResponse.postal.code;

Upvotes: 1

Related Questions