Logical Operator


#1


https://www.codecademy.com/zh/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661#


i got some troubles on my codes, can't find where i made it wrong, can anyone help me figure it out?


var user = prompt("What;s your name?").toUpperCase();

switch(user) {
    case 'Allen':
      var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
      var real = prompt("You sure your name is Allen(Yes or No)").toLowerCase();
      if (relatives === "Yes" && real === "Yes") 
      {
          console.log("cool");
      }
      else {
          console.log("Fine");
      }
     break;
    case 'Jones':
      var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
      var real = prompt("You sure your name is Jones(Yes or No)").toLowerCase();
      if (relatives === "Yes" && real === "Yes") 
      {
          console.log("cool");
      }
      else {
          console.log("Fine");
      }
      break;
    case 'Jean':
      var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
      var real = prompt("You sure your name is Jean(Yes or No)").toLowerCase();
      if (relatives === "Yes" && real === "Yes") 
      {
          console.log("cool");
      }
      else {
          console.log("Fine");
      }
      break;
      default:
      console.log("That's a nice name!");
      break;
}


#2

.toUpperCase() makes user all upper case letters. Then when you compare to your cases, none of them will be true. For example, 'Allen' != 'ALLEN'.

I tested your code by putting this line just under your prompt() line:

console.log(user)

This is a great way to debug. You can use console.log() to check in on the state of your variables throughout your code at runtime!


#3

Well, thanks a lot ! I have already deleted the uppercase code, but still find it unpassed, dunno which part I made another false again


#4

Well, after deleting the "uppercase" code

var user = prompt("What's your name?");

switch(user) {
case 'Allen':
var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
var real = prompt("You sure your name is Allen(Yes or No)").toLowerCase();
if (relatives === "Yes" && real === "Yes")
{
console.log("cool");
}
else {
console.log("Fine");
}
break;
case 'Jones':
var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
var real = prompt("You sure your name is Jones(Yes or No)").toLowerCase();
if (relatives === "Yes" && real === "Yes")
{
console.log("cool");
}
else {
console.log("Fine");
}
break;
case 'Jean':
var relatives = prompt("Do you have any relatives?(Yes or No)").toLowerCase();
var real = prompt("You sure your name is Jean(Yes or No)").toLowerCase();
if (relatives === "Yes" && real === "Yes")
{
console.log("cool");
}
else {
console.log("Fine");
}
break;
default:
console.log("That's a nice name!");
break;
}

console.log(user);


#5

Thanks everyone, i finally understand what I have misunderstood!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.