Kim
Kim

Reputation: 395

how to add logical operators?

I have been trying to work this issue of on Codecademy all day. Nothing I do seems to work.

Instructions: "Add some if/else statements to your cases that check to see whether one condition and another condition are true, as well as whether one condition or another condition are true. Use && and || at least one time each."

This is the code I previously entered:

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

switch(user) {
    case 'Sam':
        console.log("Hi, Sam");
        break;
    case 'John':
        console.log("Hi, John");
        break;
    case 'Mary':
        console.log("Hi, Mary");
        break;
    default:
        console.log("I don't know you. What is your name?");
}

Upvotes: 1

Views: 336

Answers (2)

floribon
floribon

Reputation: 19193

Your code doesn't have any if/else statements. How about something like this:

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

// If user is known, greet him
if (user === "SAM" || user === "JOHN" || user === "MARY") {
  console.log("Hi, " + user)
}
// Otherwise apologize because we are polite
else {
  console.log("Sorry " + user + ", I don't know you.");
}

// If user is neither John nor Sam, ask about them
if (user !== "JOHN" && user !== "SAM") {
  console.log("By the way, how are John and Sam doing?");
}

Upvotes: 1

Jarod
Jarod

Reputation: 495

  1. .toUpperCase() transforms a string to uppercase letters, so it would be ‘SAM’, ‘JOHN’ etc.

Change it to something like this:

String.prototype.capitalizeFirstLetter = function() {
    return this.charAt(0).toUpperCase() + this.slice(1);
}

var user = prompt("What is your name?").capitalizeFirstLetter()
  1. What conditions do you need to test?

    switch(user) {
    case 'Sam':
        if ( someCondition || anotherCondition ) { 
                // do something
        } else { // do something 
        }
        console.log("Hi, Sam");
        break;
    
    case 'John':
        console.log("Hi, John");
        break;
    
    case 'Mary':
        console.log("Hi, Mary");
        break;
    
    default:
        console.log("I don't know you. What is your name?”);
    }
    

Upvotes: 0

Related Questions