OR operator wont log false


#1

The OR operator that I used in this game for my 3rd option refuses to log false in my if statement no matter what, I can answer "no" to both of the prompts while I have var NO = false; and it still gives me the outcome a if one of the prompts were true.

I included the entire game just for lolz, the only problem comes with option 3.

var YES = true;
var NO = false;
var user = prompt("Pretend you're in a Zombie apocolypse. You as well as a few others happened to be lucky enough to be in a store that sells food, as well as firearms, and everyday building materials. The group appoints you as leader for your wits. It's your job to decide what to do. You run through all the options in your head and come up with 3 different plans. They are as follows. Option 1.) Board off all the openings to the store and live off of the supplies in the store for as long as possible. Option 2.) Gather as many firearms and ammunition as you can carry, and proceed to fight your way through the crowd of Zombies. Option 3.) Head to the roof to start a fire as a signal to any other survivors, or any rescue Helicopters. What would you choose(1, 2, or 3)? Lets see how long you can survive.");

switch(user){
    case '1':
        var survival = prompt("Are you confident that you are the only survivors left, or there are too few to sustain life(YES or NO)?").toUpperCase();
        var fallout = prompt("Are there no fallout shelters that you could manage to transport substantial amounts of food and drinks to safely(answer YES or NO)?").toUpperCase();
        if(survival&&zombies){
            console.log("While most people wouldn't agree with you this actually isn't a bad idea, assuming most of the worlds population is gone and the outbreak isn't specific to only your region. The worlds nuclear reactors will have no one to run them, and they will melt down in no time at all. Death is inevitable, at least you've managed to stay comfortable the longest. depending on how much food/water is in the store and when everything expires will determine how you die, from starvation/dehydration, or radiation poisoning. Congradulations you managed to survive the longest given the listed circumstances.");}
        else{
            console.log("So you're just going to give up even though survival is clearly possible? Well that group chose the wrong person to lead them... You clearly have an option that would allow you to live out the rest of your days!");}
        break;
    case '2':
        var place = prompt("Do you have a viable plan of action once your team leaves that store(answer YES or NO)?").toUpperCase();
        var survival = prompt("Are there enough people left on earth to sustain life? Or is it a pointless fight(answer YES if there are enough people, answer NO if it is just a pointless fight").toUpperCase();
        if(place&&survival){
            console.log("If your team has the skills and the knowledge perhaps you will survive!");}
            else{
                console.log("You must watch a lot of television/play a lot of video games. This option might seem fun and badass, but what happens when you run out of ammunition or your gun jams up? And how are you going to manage to take down every Zombie around you before they reach you? Your effort is futile, there is no way you can survive. Also if there is no one left to run the nuclear powerplants they will surely meltdown, at that point you could only survive if you were in a fallout shelter. You are most likely to die the quickest with this option.");}
        break;
    case '3':
        var evidence = prompt("Is there evidence of enough people left to have a good shot at sustaining life? Perhaps you've heard things on the radio, or seen them on the televisions in the store(answer YES or NO).").toUpperCase();
        var local = prompt("Can you positively say you have a good shot at being rescued? Perhaps you've heard a Helicopter fly past, or the radio/television broadcasted a message stating that there are search parties looking for survivors(answer YES if any of these are true, answer NO if none are true).").toUpperCase();
        if(evidence || local){
            console.log("this option could play out, or it could lead to your demise. If there just so happens to be an extremely large group of humans left that could fight their way to you, or a Helicopter that could rescue you, then you've made it out alive.");}
        else{
            console.log("In the case that you are the only survivors left, or no one is nearby to see the smoke signal/want to venture out to see what it is, and there are no rescue Helicopters that would make it to you in time, you will die. If not from starvation/thirst while staying atop the roof, from the Zombies that will wander into the store, whether it be that they manage to make it to the roof, or that you're caught venturing back into the store for food/water. You are most likely to live longer than option 2, but shorter than option 1.");}
        break;
    default:
        console.log("I'm sorry, " + user + " wasn't a valid answer. Please run the code again, this time choosing 1, 2, or 3 for your answer to the question.");
};

#2

Hi i see different things:

First

var YES = true;
var NO = false;
// for what do you use that?

Second

if(survival&&zombies){ 
// zombies?  where you define this var? With that we never get in your if  statement
// and every time in your else statement

Third
Please learn more about logical operators

a1 = true  && true      // t && t returns true
a2 = true  && false     // t && f returns false
a3 = false && true      // f && t returns false
a4 = false && (3 == 4)  // f && f returns false
a5 = "Cat" && "Dog"     // t && t returns "Dog"
a6 = false && "Cat"     // f && t returns false
a7 = "Cat" && false     // t && f returns false
a8 = "" && false        // returns ""
a9 = false && ||        // returns false

o1 = true  || true       // t || t returns true
o2 = false || true       // f || t returns true
o3 = true  || false      // t || f returns true
o4 = false || (3 == 4)   // f || f returns false
o5 = "Cat" || "Dog"      // t || t returns "Cat"
o6 = false || "Cat"      // f || t returns "Cat"
o7 = "Cat" || false      // t || f returns "Cat"
o8 = ""    || false      // returns false
o9 = false || ""         // returns ""

Fourth
A variable with a string is true. you fill your var evidence with a prompt that means it is "true"

if(evidence || local){

You will go every time in the if Statement.

I think it would be better you check the values of the variables here and not the booelan type


#3

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