dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: Would you help a complete beginner? (rock-paper-scissors-lizard-spock game)

  1. #1
    Join Date
    Jan 2014
    Posts
    1

    Question Would you help a complete beginner? (rock-paper-scissors-lizard-spock game)

    Hello!
    i just started to learn Javascript and i tried to write a rock - paper - scissors - lizard - Spock game,
    it doesn't return the right answers.
    what have i done wrong? the nesting? the math.random()?

    here's the code:

    var userChoice = prompt("Do you choose rock, paper, scissors, lizard or spock?");
    var computerChoice = Math.random();
    console.log(computerChoice);
    if (computerChoice < 0.20)
    computerChoice = "rock";
    else
    if(computerChoice <= 0.40)
    computerChoice = "paper";
    else
    if(computerChoice <= 0.60)
    computerChoice = "scissors";
    else
    if(computerChoice <= 0.80)
    computerChoise = "lizard";
    else
    if(computerChoice <= 1)
    computerChoice = "spock";


    var compare = function(choice1 , choice2){
    if (choice1 == choice2)
    return("The result is a tie!");

    else
    if (choice1 == "rock")
    if (choice2 == "scissors")
    return("rock wins");
    else
    if(choise2 == "paper")
    return("paper wins");
    else
    if(choice2 == "lizard")
    return("rock wins");
    else
    if(choice2 == "spock")
    return ("spock wins");
    else
    if (choice1 == "paper")
    if (choice2 == "rock")
    return("paper wins");
    else
    if(choice2 == "scissors")
    return("scissors wins");
    else
    if(choice2 == "lizard")
    return ("lizard wins");
    else
    if(choice2 == "spock")
    return("paper wins");
    else
    if (choice1 == "scissors")
    if (choice2 == "rock")
    return("rock wins");
    else
    if(choice2 == "paper")
    return("scissors wins");
    else
    if(choice2 == "lizard")
    return("scissors wins");
    else
    if(choice2 == "spock")
    return("spock wins");
    else
    if (choice1 == "lizard")
    if (choice2 == "rock")
    return("rock wins");
    else
    if(choice2 == "paper")
    return("lizard wins");
    else
    if(choice2 == "scissors")
    return("scissors wins");
    else
    if(choice2 == "spock")
    return("lizard wins");
    else
    if (choice1 == "spock")
    if (choice2 == "rock")
    return("spock wins");
    else
    if(choice2 == "paper")
    return("paper wins");
    else
    if(choice2 == "lizard")
    return("lizard wins");
    else
    if(choice2 == "scissors")
    return("spock wins");
    };

    compare(computerChoice, userChoice);

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    I have always hated the answers like the one I'm going to give to you but the fact that this issue (making rock-paper-scissors game) was already discussed many times before I would suggest you use the search function of the forum.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,866
    Yep, not the first, nor the last and in the spirit of the great Spock, go forth and you will find what you seek.
    STOP using $ prefix on JavaScript variable names...
    Please remember to wrap any code you have in forum tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles