5. Logical Operators


#1



Hi, in this exercise the code after asking for the class it ask are you sure 3 times and after that it sends the error: yes not defined can someone help?


var user = prompt("Choose your class: Warrior/Mage/Rogue").toLowerCase();
var war_ans = prompt("Are you sure? y/n").toLowerCase();
var mag_ans = prompt("Are you sure? y/n").toLowerCase();
var rog_ans = prompt("Are you sure? y/n").toLowerCase();
var char_select = false;
var char_error = false;
switch (user) { 
    case 'warrior':
    console.log("Your name is Olaf, and you come from a nordic coastal village.");
    if (war_ans === yes || y) {
        console.log("Ok let the adventure begin!");
        var char_select = true;
    } else { 
        console.log("Restart the Game...");
        var char_error = true;
    }
    break;
    
    
    case 'mage':
    console.log("Your name is Veigar, and you come from a gnomic village.");
    if (mag_ans === yes || y) {
        console.log("Ok let the adventure begin!");
        var char_select = true;
    } else { 
        var char_error = true;
        console.log("Restart the Game...");
    }    
    break;
    
    case 'rogue':
    console.log("Your name is Zed, and you come from a mountain village.");
    if (rog_ans === yes || y) {
        console.log("Ok let the adventure begin!");
        var char_select = true;
    } else { 
        var char_error = true;
        console.log("Restart the Game...");
    }    
    break;
    
    default:
    console.log("Please choose one of the classes.");
};

if (char_select === true && char_error === false) {
    var prob = prompt("A beast is in a attacking stance what you do?");
};


#2

here:

mag_ans === yes || y

(and many other instances) yes and y are strings, not variables, so they should surrounded by quotation marks. And when use or (||) or and (&&) operator comparison is done at both sides of the operator


#3

Thanks but how do i stop it from asking 3 times if im sure?
(I doesnt say yes not defined anymore)


#4

don't make 3 prompts if you don't wanted to be prompted 3 times


#5

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