9/9 Next step. I want your oppinion


#1

Hi there. I am bit into coding and i was experimenting some, and i could use your opinion, did I do it good and, what i could do better? Code works, take a look.
Its rock, paper, scissors, lizard, Spock, just like in The Big Bang Theory.

var userChoice = prompt("Pick: rock, paper, scissors, lizard, Spock!");
var computerChoice = Math.random();

if (computerChoice < 0.21) {
computerChoice = "rock";
}else if (computerChoice < 0.41) {
computerChoice = "paper"
}
else if (computerChoice < 0.61) {
computerChoice = "scissors"
}
else if (computerChoice < 0.81) {
computerChoice = "Spock"
}
else {
computerChoice = "lizard"
}

console.log("Computer: " + computerChoice);

var compare = function (choice1, choice2) {

if (choice1 === choice2) {
return "Result is a tie!"
}
else if (choice1 === "rock") {
if (choice2 === "scissors") {
return "rock crushes scissors"}
else if (choice2 === "paper") {
return "paper covers a rock"}
else if (choice2 === "Spock") {
return "Spock vaporizes rock"}
else {
return "rock crushes lizard"}
}

else if (choice1 === "paper") {
if (choice2 === "scissors") {
return "scissors cuts paper"}
else if (choice2 === "rock") {
return "paper covers rock"}
else if (choice2 === "Spock") {
return "paper disporoves Spock"}
else {
return "lizard eats paper"}
}

else if (choice1 === "scissors") {
if (choice2 === "rock") {
return "rock crushes scissors"}
else if (choice2 === "paper") {
return "scisors cuts paper"}
else if (choice2 === "Spock") {
return "Spock smashes scissors"}
else {
return "scissors decapitates lizard"}
}

else if (choice1 === "Spock") {
if (choice2 === "scissors") {
return "Spock smashes scissors"}
else if (choice2 === "paper") {
return "Paper disporoves Spock"}
else if (choice2 === "rock") {
return "Spock vaporizes rock"}
else {
return "lizard poisons Spock"}
}

else if (choice1 === "Lizard") {
if (choice2 === "scissors") {
return "scissors decapitates lizard"}
else if (choice2 === "paper") {
return "lizard eats paper"}
else if (choice2 === "Spock") {
return "lizard poisons spock"}
else {
return "rock crushes lizzard"}
}
};
compare(userChoice,computerChoice);


#2

Looks good. Two things you could add:

  1. Put in a condition if it's a tie to prompt the user to enter in a different choice so each round has either choice1 or choice2 winning rather than it resulting in just a tie.
  2. Put in console.log("Your choice: " + userChoice); to also show what the user inputted rather than just showing what the computer chose at the end of the game.

#3

@strux,

1 To extend the userChoice-prompting with a validity-check, you would
embed the prompting into a function-body...
So you would start by
var getUserChoice = function() { your prompting here };

2 If you are extending the choice-possibillity, you have to extend the
so-called truth-table.

Your new Decisions-Table: fill-in ?? wins...

              | ch-2  |  ch-2  | ch-2     | ch-2   |
              | rock  |  paper | scissors | rope   |
  ------------|-------|--------|----------|--------|
              |       | paper  |  rock    | ????   |
 choice1  rock|   X   | wins   |  wins    |  wins  | (1st else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              | paper |        |scissors  |  ? ?   |
 choice1 paper| wins  |    X   |  wins    |  wins  | (2nd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  rock |scissors|          |  ?  ?  | 
 ch-1 scissors|  wins | wins   |   X      |  wins  | (3rd else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|
              |  ? ?  | ? ? ?  |  ? ? ?   |        | 
 ch-1 rope    |  wins | wins   |   wins   |    X   | (4th else if )
              |       |        |          |        |
  ------------|-------|--------|----------|--------|

3 What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

    if (theResult === "It is a tie!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop
 }; //closing the function-body of playTheGame

// call the function playTheGame
playTheGame();

#4

It's mentioned in the exercise, but it is a lot easier if you have a sneak peek at loops especially while and do/while loops as @leonhard_wettengmx_n shows in his post.

Actually the computerChoice output was just meant to be a debugging feature so that you see what the computer picked and can compare the result to what you would expect it to be. This is a line that can be deleted once your ready. Or you make the output totally official and post e.g.

console.log(userChoice +" vs " + computerChoice);
console.log("Let's get ready to rumble!");
console.log(compare(userChoice,computerChoice);

#5

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} console.log("Computer: " + computerChoice);
var compare = function (choice1, choice2) {
if (choice1 === choice2) {
prompt("It's a tie! Choose again!")
} else if (choice1 === "rock") {
if (choice2 === "scissors") {
return"rock wins";
} else {
return"paper wins";
}
} else if (choice1 === "paper") {
if (choice2 === "rock") {
return "paper wins";
} else {
return"scissors wins";
}
}
}; else if (choice1=== "scissors") {
if (choice2==="rock") {
return "rock wins";
}; else {
return "scissors wins";
}
}
};

can anyone help? A syntax error that says unexpected keyword else keeps coming up.


#6

@alexamarkk,

You are using semi-colon's-; in your IF ELSE-IF statement structure....

In Javascript a semi-colon-; is an End-of-Statement indicator....
causing the rest of the =statement= not being evaluated.

Please read
http://www.codecademy.com/forum_questions/559dcb4be39efe550f00006b
and concentrate on:

  • the creation of the compare function
  • the truth-table
  • the build of the IF ELSE-IF statement structure as part of the compare FUNCTION-BODY
  • the execution of the compare function (keeping in mind that the return statement is used )