简体   繁体   中英

How can I prevent console from printing in JavaScript game?

I've been practicing a game with JavaScript, and was wondering how to prevent the console from printing when a user makes an ill defined choice?

Here is the code:

var user = prompt("Do you choose rock, paper or scissors?");
var computer = Math.random();
if (computer < 0.34) {
    computer = "rock";
}
else if (computer <= 0.67) {
    computer = "paper";
}
else {
    computer = "scissors";
}
console.log("Computer Chooses: " + computer);
console.log("User Chooses: " + user);
var compare = function (computer, user) {
    if (computer === "rock") {
        if (user === "scissors") {
            return "Computer wins by choosing rock!";
        }
    }
    else if (computer === "scissors") {
        if (user === "paper") {
            return "Computer wins by choosing scissors!";
        }
    }
    else if (computer === "paper") {
        if (user === "rock") {
            return "Computer wins by choosing paper!"
        }
    }
    if (computer === user) {
        return ("It is a tie!")
    }
    else if (user === "paper") {
        if (computer === "rock") {
            return ("You win by choosing paper!")
        }
    }
    else if (user === "rock") {
        if (computer === "scissors") {
            return ("You win by choosing scissors!")
        }
    }
    else if (user === "scissors") {
        if (computer === "paper") {
            return ("You win by choosing scissors!")
        }
    }
    ***if (user !== "rock" && user !== "paper" && user !== "scissors") {
        confirm(user + " is an invalid entry.");
    }***
};
compare(computer, user);

At the end I snipped the bit of code that gives the user an indication that he has put in the wrong characters. What I am wondering is:

How do I keep anything from displaying to the console once someone has put in the wrong input?

make the validation of user input at the beginning . if user passes only show the code else show error.

var user = prompt("Do you choose rock, paper or scissors?");
if(user !== "rock" && user !== "paper" && user!== "scissors") { 
    confirm(user + " is an invalid entry."); 
} else {
    // code for process
}

One option would be to keep asking the user for a valid input until a valid input is given:

while (user != "rock" && user != "paper" && user != "scissors") {
    user = prompt("Do you choose rock, paper or scissors?")
    if (user == null) {
        break;
    }
};

if (user != null) {
...
}

http://jsfiddle.net/2w3pt5yy/3/

You can add this code on top of your js code

var console = {};
console.log  = function(){};

You have more if statements than is necessary; you can simplify the logic.

var getComputerMove = function () {
    // equivalent of Math.floor
    // aka it grabs the integer component of `Math.random() * 3`
    // either 0, 1, or 2
    return (Math.random() * 3) | 0;
}

var playRockPaperScissors = function () {
    var moves = [ "rock", "paper", "scissors" ]
      , user = ""
      , computer = getComputerMove();

    // while `user` is not in the `moves` array
    // `Array.indexOf` returns -1 if an element is not in the array
    // prompt the user for his move
    while (moves.indexOf(user.toLowerCase()) == -1) {
        user = prompt("Rock, paper, or scissors?");
    }

    // this is where you can save yourself all that typing and think out
    // the possible movesets:
    // item:    rock < paper < scissors < rock
    // index:   0    < 1     < 2        < 0
    // so, the computer wins if its choice is greater than the user's,
    // or if the computer chose "rock" and the user chose scissors
    // we can translate this to
    // user < computer || (computer == 0 && user == 2)


    var userIndex = moves.indexOf(user.toLowerCase());

    // uncomment, if you want to see the moves
    // console.log("user:", user, "computer:", moves[computer]);

    if (userIndex < computer || (userIndex == 2 && computer == 0) {
        alert("Computer wins!");
    } else {
        alert("User wins!");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM