Matt
Matt

Reputation: 21

Switch statement goes directly to default even with breaks

What I'm attempting to do is to create a switch statement that takes data from the JSON API and prints it depending on the location of the user.

For example, if the user is located in the US, then it prints only the city and state. If located outside of the US, it prints the city and the country. If the city is an empty string, then it prints only the country.

As of now, no matter what location I set my VPN to, it goes to the default case(city and country).

  var location;
  var city;
  var state;
  var country;

  $.getJSON("http://ipinfo.io", function(data) {
    console.log(data);
    location = data.loc;
    city = data.city;
    state = data.region;
    country = data.country;

    switch (data) {
      case country === 'US':
        $("#yourCity").html(city + ", " + state);
        break;
      case city === "":
        $("#yourCity").html(country);
        break;
      default:
        $("#yourCity").html(city + ", " + country);
    }

I chose to use a switch statement rather than an if statement because I may want to add more cases later on down the line.

Any help would be greatly appreciated.

Upvotes: 2

Views: 124

Answers (3)

Ousmane D.
Ousmane D.

Reputation: 56453

You're not using the switch case properly.

Basically, you need to pass a specific type of data. Currently, the switch variable is not a country nor a city hence it goes straight to the default.

further reading:

JavaScript Switch Statement

Upvotes: 0

Channa Jayamuni
Channa Jayamuni

Reputation: 1905

Switch-case statement can be used only to compare one value with several others. According to your code sample, you have tried compare both country and city using switch case. That is not possible, definitely you'll have to use if-else statement handle this.

Please consider following method.

  var location;
  var city;
  var state;
  var country;

  $.getJSON("http://ipinfo.io", function(data) {
    console.log(data);
    location = data.loc;
    city = data.city;
    state = data.region;
    country = data.country;

    if(country === 'US'){
        $("#yourCity").html(city + ", " + state);
    }else{
        if(city===''){
            $("#yourCity").html(country);
        }else{
            $("#yourCity").html(city + ", " + country);
        }
    }

You also used switch case statement in a wrong way. Switch case statements doesn't have arithmetic compare symbols. Try switch case statement as follows.

var day = new Date().getDay();
switch (day) {
    case 4:
    case 5:
        text = "Soon it is Weekend";
        break; 
    case 0:
    case 6:
        text = "It is Weekend";
        break;
    default: 
        text = "Looking forward to the Weekend";
} 

Upvotes: 1

orange cat
orange cat

Reputation: 41

You need to pass something more specific than just data in your switch statement. Since data is an object and not country or city it goes straight to the default.

I think you are thinking that when it goes through the case it looks through the object and sees if it has the value country or city, but it doesn't. You need to pass something like switch(data.location) and then data.location value is either country or city. So you might need to move things around a bit. Hope that makes sense!

Upvotes: 4

Related Questions