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!
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.
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..
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.
?? 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.
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.
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.
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.
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.