www.webdeveloper.com
Results 1 to 4 of 4

Thread: First Javascript Code

  1. #1
    Join Date
    Sep 2013
    Posts
    2

    First Javascript Code

    Hi All,

    I've recently been teaching myself Javascript, I came across a website codeacademy which has really helped out. Although one exercise is to create a Rock, Paper, Scissors game. I can imagine its really simple stuff but we all have to start somewhere, anyhow, I completed the exercise on codeacademy but when I copied the code into a html doc within the script tags (I also tried running the code in the Firebug console) it doesn't seem to get passed the first prompt. The prompt appears, I type in a selection i.e. rock but then the code stops there and nothing else happens

    Can anyone help me with the code? Is there something missing for it not to run properly?


    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";
    }


    var compare = function (choice1 , choice2) {
    if (choice1 === choice2) {
    return "The result is a tie!";
    }
    if (choice1 === "rock") {
    if (choice2 === "scissors") {
    return "rock wins";
    } else if (choice2 === "paper") {
    return "paper wins";
    }

    if (choice1 === "paper") {
    if (choice2 === "rock") {
    return "paper wins";
    } else if (choice2 === "scissors") {
    return "scissors wins";
    }
    if (choice1 === "scissors") {
    if (choice2 === "rock") {
    return "rock wins";
    } else if (choice2 === "paper") {
    return "scissors wins";
    }
    }
    }
    }
    };


    I really appreciate your help

    Cheers guys
    James

  2. #2
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377

    Lightbulb

    Well, it has done all you have told it to do, so it stops.

    After the prompt, the rest is a function that does not get called.

    BTW, you should enclose your script between [ code] and [ /code] tags (without the spaces)
    to make it easier to read and test your code. It also retains your formatting and indentation.

    See what happens when you properly indent. You will see that the function is never executed.
    Code:
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8" />
    
    <title> Untitled </title>
    
    </head>
    <body>
    <script type="text/javascript">
    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";
      }
    
    
    var compare = function (choice1 , choice2) {
      if (choice1 === choice2) {
        return "The result is a tie!";
      }
      if (choice1 === "rock") {
        if (choice2 === "scissors") {
          return "rock wins";
        } else
          if (choice2 === "paper") {
            return "paper wins";
          }
        if (choice1 === "paper") {
          if (choice2 === "rock") {
            return "paper wins";
          } else
            if (choice2 === "scissors") {
              return "scissors wins";
            }
          if (choice1 === "scissors") {
            if (choice2 === "rock") {
              return "rock wins";
            } else
              if (choice2 === "paper") {
                return "scissors wins";
              }
          }
        }
      }
    };
    
    </script>
    
    </body>
    </html>
    Last edited by JMRKER; 09-10-2013 at 10:36 PM.

  3. #3
    Join Date
    Sep 2013
    Posts
    2
    Hi JMRKER, thank you for your response. I'll remember to add the [code] tag in future, thanks for letting me know.

    Ok so I added
    Code:
    compare(userChoice,computerChoice)
    Which seemed to work

    Thanks again

    James

  4. #4
    Join Date
    Dec 2005
    Location
    FL
    Posts
    7,377
    Quote Originally Posted by jamessk View Post
    Hi JMRKER, thank you for your response. I'll remember to add the [code] tag in future, thanks for letting me know.

    Ok so I added
    Code:
    compare(userChoice,computerChoice)
    Which seemed to work

    Thanks again

    James
    You're most welcome.
    Happy to help.
    Good Luck!

Thread Information

Users Browsing this Thread

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

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