Alternative way to code the game


#1

Hello, at one point I've just started writing my own code before reading further instructions. Then I compared it with the ones provided by codeCademy. They both works but are slightly different. Just wanted to share mines. If you have any suggestions for improvement, please share them with me, Peace! :slight_smile:


var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();

if (computerChoice <= 0.33) {
    computerChoice = "rock";
    console.log("Computer Choice is " + computerChoice);
}
else if (computerChoice <= 0.66 ) {
    computerChoice = "paper";
    console.log("Computer Choice is " + computerChoice);
}
else {
    computerChoice = "scissors";
    console.log("Computer Choice is " + computerChoice);
}
console.log(" ");
   
    function compare(choice1,choice2) {
        if (userChoice === computerChoice) {
            console.log ("It's a draw");
        }
        else if ( ((choice1 === "paper") & (choice2 === "rock")) & 
                ((choice1 === "rock") & (choice2 === "scissors")) &
                ((choice1 === "scissors") & (choice2 ==="paper")) ) {
                    console.log ("You win! Congratulations!");
        }
        else {
            console.log ("You loose. Computer wins.");
        }    
    }
    
    compare(userChoice,computerChoice);


#2

i am not agreeing with your solution? I can either play a draw, or loose. It is impossible to win in your game.

you do know that & is a bitwise operator? documentation

I can see what you are trying to do, but then you would get:

else if ((choice1 === "paper" && choice2 == "rock") || (choice1 === "rock" && choice2 == "scissors"))

then you will need and (&&) and or (||) operators


#3

Thank you Stetim! You are right. I havent got to know the right operators ( && || ) before I started this excercise. I think they are pretty important and should be included in lessons before this one. Peace!


#4

The solution the exercise uses can be done without the || and && operator. Yes, if you want to write an alternative solution you might run into trouble


#5

What is this line for?


#6

It supposed to work as < B R > on the console, so that all the text would not display line by line (looked unclear).


#7

moreover it represents a space.


#8

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