Using the logical operators in the game, how?


#1

Hi, I'm trying to finish this step, but having a bit of an issue with the using the logical operators in my game.

In the way I've built it, I can't see where I can add conditions which will become true or false?

Here's my code:

var user = prompt("You're in the middle of a crossroads, do you go left, 
right, straight or back?").toLowerCase();

switch (user) {
    case 'left':
    console.log("You go left");
    break;
    case 'right':
    console.log("You go right");
    break;
    case 'straight':
    console.log("You go straight");
    break;
    case 'back':
    console.log("You go back");
    break;
    default:
    console.log("Gotta go somewhere, try again...");
}

I know how to actually place the operators and the if/else statements, but not what to do with them based on my inputs.

Any ideas? Thanks


#2

you can make more prompts. And then use them in your if/else statement with logical operators


#3

Thanks for the reply, would you care to show one example please?

Thanks


#4

case 'straight':
    console.log("You go straight");
    var scission = prompt("you come at scission, take a left or right?").toLowerCase();
    if (scission == "left"){//do something}
    break;

you can also do two prompts, and then use && or ||, if you need them.


#5

why do i compare with "yes"? That doesn't make any sense, that should obviously be left (or right)


#6

Thanks for the reply :slight_smile:

With your help I made this much more elaborate by creating an additional switch inside the existing switch and inserted more prompts and an if/else if/if statement.

I know it doesn't make 100% sense in terms of game logic, but the code works well, which is what matters :slight_smile:

var user = prompt("You're in the middle of a crossroads, do you go left, right, 
straight or back?").toLowerCase();

switch (user) {
    case 'left':
    console.log("You go left");
    var userLeft = prompt("You see a house, do you go in?").toLowerCase();
        if (user === "left" && userLeft === "yes") {
            console.log("You went left and entered the house!");
        } else { console.log("You went left but didn't enter the house");
        }
    break;
    case 'right':
    console.log("You go right");
    var userRight = prompt("You go right...");
    var takeDog = prompt("You see a dog, do you take it with you?");
        switch (takeDog) {
            case 'yes':
            alert("Yay, you like dogs!");
            break;
            case 'no':
            alert("Guess you don't like dogs...");
            break;
            default:
            alert("Yes or no?");
        } if (user === "right" || takeDog === "yes") {
            console.log("You either went right, or took the dog");
        } else if (user === "right" || takeDog === "no") {
            console.log("You either went right, or didn't take the dog");
        } else {
            console.log("Not sure what you did...");
        }
    break;
    case 'straight':
    console.log("You go straight");
    var userStraight = prompt("You find a ball, do you take it?");
    break;
    case 'back':
    console.log("You go back");
    var userBack = prompt("You see a girl, do you talk to her?");
    break;
    default:
    console.log("Gotta go somewhere, try again...");
}

BTW this is only my 2nd day with coding so any feedback is appreciated.

Thanks


#7

Good to hear the game works :slight_smile: For now, just continue the course, then you can improve your code later on. You could of course implant a while loop, to check the user actually entered something valid.

Wait, did the course also teach a do while loop? It is ideal for user input and validation, lets say i want the user to enter left or right:

var scission = prompt("you come at scission, take a left or right?").toLowerCase();

i can put this in a do while loop:

do {
var scission = prompt("you come at scission, take a left or right?").toLowerCase();
} while( scission != "left" && scission != "right" )

this way, you are sure the user enters something valid. (it will just keep prompting, till the user enters something valid)

How does this work? If the user enters something invalid (for example straight both condition are true (straight does not equal left, which is true), true && true is true, the loop will continue to run (the while loop will run until the condition is false)

so, if the user enters left, left != left is false, and left != right is true. true && false is false, the loop ends, the user enter something valid


#8

Thanks! the course did teach the do/while loop, but I didn't think of using that.

I'll try that on one of the other choices now and post the code.


#9

    case 'straight':
    console.log("You go straight");
    do {
        var userStraight = prompt("You come at a scission, 
        left or right?").toLowerCase();} 
        while (userStraight != "left" && userStraight !="right");
        
        if (userStraight === "left") {
            console.log("You went straight and then left");
        } else {
            console.log("you went straight and then right");
        }
    break;

Thanks for the help!


#10

BTW another logical question:

Why is true && false always false, but true || false is always true?

Why is true stronger in ||, but weaker in &&?


#11

take a look at this table (you need to scroll down a bit):

a1 = true  && true      // t && t returns true
a2 = true  && false     // t && f returns false
a3 = false && true      // f && t returns false

and:

o1 = true  || true       // t || t returns true
o2 = false || true       // f || t returns true
o3 = true  || false      // t || f returns true

the || is the or operator. In case of the or operator, one of the conditions has to be true (one condition has to be true, or the other condition must be true), in case of the and operator (&&) both conditions have to be true (the first and the second condition have to be true). false && false and false || false are both always false.

of course, keep in mind when you use a not (!) operator. You can make the chain really long, you know limit it to two options, but you could do more, remember the rock paper scissors game? You can also use a do while loop and the && operator to check the user gives you valid input (3 options, but same principal)