Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-17-2012, 11:15 PM   PM User | #1
piemanXD
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
piemanXD can only hope to improve
Hi Guys! 2nd day in the world of Java, and tearing my hair out with this. Any help ?

hmmmkay. Basically this is a simple rock paper scissors game just to get myself familiar with basic functions and statements.

var userInput = prompt("Please choose rock, paper or scissors. Lowercase.");
var cpuInput = Math.random();

if (cpuInput <= 0.34){
cpuInput = "rock";
}else if(cpuInput >= 0.67){
cpuInput = "paper";
}else{ cpuInput = "scissors";}

var compare = function(Choice1,Choice2){

if (Choice1===Choice2){
return "It's a draw!";
}

if (Choice1==="rock") {
if (Choice2==="paper") {
return "Player wins!";
} else {
return "Computer picks paper and wins!";
}
if (Choice1==="paper") {
if (Choice2==="scissors") {
return "Player wins!";
}else{
return "Computer picks scissors and wins!";
}

if (Choice1==="scissors"){
if (Choice2==="rock"){
return "Player wins!";
}else{
return "Computer picks rock and wins!";
}
}
}
}
};
compare(userInput,cpuInput);


Now, here's the problem with it. It only accepts a draw for either scissors or paper. The rock works perfectly and will show the correct outcome each time, but rock and paper only show the draw or nothing at all. I'm doing this on the codeacdemy web app if that makes a difference. The first statement is fine, as you can see if you add a console.log line - it's selecting as required. PLEASE HELP!
piemanXD is offline   Reply With Quote
Old 12-18-2012, 12:01 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
First of all, this is *NOT* your "2nd day in the world of Java."

Unless I miss my guess entirely, you have not yet even seen Java.

This code is JavaSCRIPT. Java and JavaScript are entirely different languages. (In general, Java is much harder to learn, certainly when getting started.)

Oh...and do yourself a *HUGE* favor: Learn to indent your code properly. If you actually did so and it just didn't show up that way here, simply use your mouse to highlight the code part of your post and then click on the "#" icon in the top of the editor window.


Anyway...

Do you care that you are using ancient history JavaScript? prompt( ) and alert( ) are considered obsolete.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-18-2012, 12:01 AM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
Your ifs are nested incorrectly and perhaps your logic is incorrect(?), but it is messy to work out unless you enclose your code in CODE tags (use the hash # sign) and indent the code neatly - so we (and you) can see clearly where the brackets open and close.

IMO prompt() and alert() are still useful tools when just starting out.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 12-18-2012 at 12:04 AM..
AndrewGSW is offline   Reply With Quote
Old 12-18-2012, 12:03 AM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Actually, your whole problem is that some of your { and } are in the wrong places.

*IF* you had properly indented your code, you would have seen this.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-18-2012, 12:11 AM   PM User | #5
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
No. I take it back. It is *NOT* just indentation.

Your code actually makes no sense.

I do have to ask: Why bother to have a separate compare function if you then name the two arguments to it just Choice1 and Choice2???

Wouldn't it be more logical to name them something like user and computer?

If you do that you see *some* of your bugs IMMEDIATELY!
Code:
function compare( user, computer )
{
    if ( user === computer )
    {
        return "It's a draw!";
    }
    if ( user  ==="rock") 
    {
        if ( computer ==="paper" ) 
        {
             return "Player wins!"; //??? rock covers paper???
        } else { 
             return "Computer picks paper and wins!"; // A FLAT OUT LIE!
       }
   }
...
Look there: The user *HAS* chosen rock.

Now you say "what happens if the computer chooses paper"?

As your code is written, you say that rock for user and paper for computer means the player wins!

BUT WORSE:

As your code is written, you say that else if the computer *DID NOT CHOOSE PAPER* (after the user chose rock) then the result is that the computer *DID* choose paper! IMPOSSIBLE! Your else says the computer DID NOT 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.
Old Pedant is offline   Reply With Quote
Old 12-18-2012, 12:12 AM   PM User | #6
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
a shortcut to get rock, paper or scissors (not entirely random, but close enough for the exercise, I reckon):

Code:
<script>
var cpuInput = ["rock","paper","scissors"][Math.floor(Math.random()*3)];
alert(cpuInput)
</script>
xelawho is offline   Reply With Quote
Old 12-18-2012, 12:25 AM   PM User | #7
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
?? Why do you say "not entirely random"? True, the random number generator in JavaScript doesn't satisfy any serious criteria, especially when it comes to sequences, repetitions, etc., but it *IS* random when making a single random selection.
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-18-2012, 12:47 AM   PM User | #8
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,168
Thanks: 59
Thanked 3,993 Times in 3,962 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Here. I know you can't turn this in, as your instructor will be expecting you to write ancient history JavaScript since he/she apparently hasn't bothered to learn modern JavaScript. But this is one way you could do it in modern JS.
Code:
<!DOCTYPE html>
<html>
<head>
<title>Rock, Paper, Scissors Game</title>
<style type="text/css">
span { font-weight: bold; color: blue; }
</style>
</head>
<body>
<form>
Make your choice:
    <input type="button" name="user" value="Rock">
    <input type="button" name="user" value="Paper">
    <input type="button" name="user" value="Scissors">
<hr>
Your choice: <span id="userChoice"></span><br>
Computer choice: <span id="computerChoice"></span><br>
Winner: <span id="winner"></span>
</form>


<script type="text/javascript">
(
  function()
  {
      var form = document.forms[0];
      var uMsg = document.getElementById("userChoice");
      var cMsg = document.getElementById("computerChoice");
      var wMsg = document.getElementById("winner");

      var btns = form.user;
      for ( var b = 0; b < btns.length; ++b )
      {
          btns[b].onclick = play; 
      }
      function play( )
      {
          var u = this.value;
          var c = ["Rock","Paper","Scissors"][Math.floor(Math.random()*3)];          
          uMsg.innerHTML = u;
          cMsg.innerHTML = c;
          wMsg.innerHTML = ( u == c ) ? "DRAW"
                         : (    u=="Paper"    && c=="Rock" 
                             || u=="Rock"     && c=="Scissors"
                             || u=="Scissors" && c=="Paper" ) ? "PLAYER"
                         : "COMPUTER";
     }
  }
)();
</script>
</body>
</html>
__________________
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.
Old Pedant is offline   Reply With Quote
Old 12-18-2012, 08:11 AM   PM User | #9
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,447
Thanks: 0
Thanked 496 Times in 488 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
Quote:
Originally Posted by AndrewGSW View Post
IMO prompt() and alert() are still useful tools when just starting out.
Yes - they are useful debugging tools for anyone who has not yet learnt where to find the error console and debugger in their browser. (or who hasn't yet installed a debugger in Firefox)

A simple HTML form is far more suitable for data input and innerHTML more suitable for outputting anything than using debugging dialog boxes are because:

1. you don't get the checkboxes asking if you want to disable further dialogs (in Firefox and Chrome) or disable JavaScript (in Opera). Those checkboxes were added once the dialogs had become obsolete for use in live scripts and were repurposed as a simple debugging tool - although actually using the error console is actually far easier.
2. Your script actually interacts with the web page as all JavaScript since 2005 can and should. Which means that you are 1000% closer to being able to write real JavaScript suitable for use on the web.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Old 12-18-2012, 01:28 PM   PM User | #10
piemanXD
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
piemanXD can only hope to improve
EDIT: I'm an idiot

Last edited by piemanXD; 12-18-2012 at 03:48 PM.. Reason: I'm an idiot with a bad temper
piemanXD is offline   Reply With Quote
Old 12-18-2012, 01:39 PM   PM User | #11
piemanXD
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
piemanXD can only hope to improve
Quote:
Originally Posted by Old Pedant View Post
Actually, your whole problem is that some of your { and } are in the wrong places.

*IF* you had properly indented your code, you would have seen this.
EDIT: I'm an idiot

Last edited by piemanXD; 12-18-2012 at 03:48 PM.. Reason: I'm an idiot with a bad temper
piemanXD is offline   Reply With Quote
Old 12-18-2012, 01:40 PM   PM User | #12
piemanXD
New to the CF scene

 
Join Date: Dec 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
piemanXD can only hope to improve
I'm sorry for being a douchebag everybody

Last edited by piemanXD; 12-18-2012 at 03:49 PM..
piemanXD is offline   Reply With Quote
Old 12-18-2012, 01:56 PM   PM User | #13
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 453 Times in 451 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
Quote:
Originally Posted by piemanXD View Post
Old pedant, if you don't want to help, don't post. Don't be a miserable prick. I'd rather do without you.
The guy rewrites your entire code for you and that's your response? I don't think you'll have to worry too much about who responds to your posts in future, silly rabbit.
xelawho is offline   Reply With Quote
Old 12-18-2012, 02:03 PM   PM User | #14
donna1
New Coder

 
Join Date: Nov 2012
Location: london
Posts: 55
Thanks: 5
Thanked 1 Time in 1 Post
donna1 can only hope to improve
Quote:
Originally Posted by piemanXD View Post
Old pedant, if you don't want to help, don't post. Don't be a miserable p...k. I'd rather do without you.
How rude. Old Pedant is the nicest guy on this site and has helped me many times.
donna1 is offline   Reply With Quote
Old 12-18-2012, 03:02 PM   PM User | #15
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by xelawho View Post
The guy rewrites your entire code for you and that's your response? I don't think you'll have to worry too much about who responds to your posts in future, silly rabbit.
Is is always sad when immature juvenile newcomers think that they can insult senior members. The inevitable outcome is a destroyed reputation meaning that no-one is going to take time to help you in the future lest they get a mouthful. You catch more flies with honey than with vinegar. When you grow up you will learn that rudeness and bad language never bring beneficial results.

Code:
var rude = true;
var thanks = false;
if (rude && !thanks) {
var interestLost = true;
var moreHelp = 0;
}
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:55 AM.


Advertisement
Log in to turn off these ads.