Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy I really need help on a coding exercise.

    Okay so I am as almost as fresh as one can be when it comes to being a n00b at JavaScript. So I please ask for your patience with this debugging question.

    I am working on this exercise from a website that teaches you how to write code in a number of different languages. Obviously the one I am currently working on is JavaScript.

    In this exercise you have to write the scripting for a simple game of rock, paper, scissors. The problem is that although everything in my coding seems correct, an error keep popping up. The error states that my choice1 is not defined.

    Below is what I have got so far. Could anyone please point out where I have gone wrong with this.

    Code:
    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";
    }
    userChoice = choice1;
    computerChoice = choice2;
    
    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==="paper") {
            return "scissors wins";}
        }
    };

  • #2
    Regular Coder
    Join Date
    Aug 2012
    Posts
    145
    Thanks
    0
    Thanked 27 Times in 27 Posts
    Without really running your code...look at this:

    Code:
    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";
    }
    userChoice = choice1;
    computerChoice = choice2;
    you first have userChoice as a prompt, thus whatever prompt is will be your userChoice variable. But then you have userChoice equal to choice1...i think you need to reverse them. So choice1 equals whatever userChoice is:

    Code:
    choice1 = userChoice;
    Last edited by Brandnew; 06-24-2013 at 08:04 PM.
    1 Corinthians 15:3-4 / Ephesians 2:8-9 - What or Who are you living for? Jesus is returning very soon

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    Quite simply, these two lines are bogus and should not be in there *AT ALL*:
    Code:
    userChoice = choice1;
    computerChoice = choice2;
    Get rid of them.

    But then you need to *SOMEPLACE* make a call to your compare function.

    No place in that code do you do so.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,014
    Thanks
    79
    Thanked 4,436 Times in 4,401 Posts
    There are other errors, but you have to call compare( ) first before you will find them.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    New to the CF scene
    Join Date
    Jun 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks you guys so much! I really appreciate it!


  •  

    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
    •