5 - logical operators (problem with nested if's)


#1

I bolded the relevant part. I'm wondering why when a user inputs "football" and then says they ARE a Dolphins fan..."Who Dat!" still alerts. Or if they say they are not a dolphins fan, it still says "Same!" before going to "Who Dat!"


var sport = prompt("What's your favorite sport? Feel free to pick two.").toUpperCase();

switch(sport){
case 'BASKETBALL':
alert("Same!");
break;
case 'FOOTBALL':
alert("cool");
var team1 = prompt("Are you a Dolphins fan? Y/N/MAYBE");
if(team1 = "Y"){
alert("Same!");}
else if(team1 = "N" || "MAYBE"){
var team2 = prompt("How about a Saint's fan?");}
if(team2 = "Y"){
alert("Who Dat?");}
else if(team2 = "N" || "MAYBE"){
alert("You suck!");}
break;
case 'BASEBALL':
alert("That's fine...");
break;
case 'Football' && 'BASEBALL':
alert("Only one, actually.");
break;
default:
alert("Really?");
break;}


#2

Hi @jfirest

I can tell you some advices :

  • In a switch, there is no "break;" after "default" (line 35)
  • "Football" should be "FOOTBALL" (line 29)
  • 'Football' && "BASEBALL" it's impossible, you should write:
    'FOOTBALL AND BASEBALL' in your case (line 29)
  • You have to put "==" not "=" in your "if" and "else if" condition
  • After team1 and team2 you have to put .toUpperCase method
  • Team2 should be in the same "else if"

Look your code in a correct syntax :

var sport = prompt("What's your favorite sport? Feel free to pick two.").toUpperCase();

switch(sport){
case 'BASKETBALL':
alert("Same!");
break;

case 'FOOTBALL':
alert("cool");
var team1 = prompt("Are you a Dolphins fan? Y/N/MAYBE").toUpperCase();

if(team1 == "Y"){
alert("Same!");
}else if(team1 == "N" || team1 == "MAYBE"){
var team2 = prompt("How about a Saint's fan?").toUpperCase;
if(team2 == "Y"){
alert("Who Dat?");
}else if(team2 == "N" || team2 == "MAYBE"){
alert("You suck!");
}
};
break;

case 'BASEBALL':
alert("That's fine...");
break;

case 'FOOTBALL AND BASEBALL':
alert("Only one, actually.");
break;

default:
alert("Really?");
};

Good train and hold on bro :wink: