Ever have an extended period of really stressful times? Well look, I'm not normally a dick like that but I'm just stressed out, and I know that doesn't give me the right to say anything like I did but hey, that's why I did it. I want to apologize to the guy who wrote out the new code for me only to have me throw it back in his face - so I'm sorry! I also want to apologize to the community at large but I guess I couldn't really have gotten off on a worst foot. Anyone who actually wants to see what I said can see it quoted in one of the posts above... once again, sorry guys and especially sorry to oldpedant.
TLDR; I'm a hot headed idiot and I apologize for that fact
As I said, you almost surely can't show my code to your instructor.
(1) He/she won't believe you wrote it.
(2) He/she quite possibly won't even understand it.
If you have the typical college instructor who only knows what is in the book and has never actually programmed for a living, he/she won't even realize that the book is 10 years or more out of date.
*********
Now, having said all the above...
You could *still* use the CONCEPT that I showed in that code in your own code.
Look carefully at how I decided who had won. All very simple. Just one big set of && and || conditions. Yes, you can use those same conditions in your own code.
Given that you apparently *must* use ugly alert( )s to output your messages, consider something like this:
Code:
var msg = "Player plays " + userInput + ", Computer plays " + cpuInput;
if ( userInput == cpuInput ) { msg += ", result is a DRAW"; }
else if ( .... see my post for the concept ... ) { msg += ", PLAYER wins"; }
else { msg += ", COMPUTER wins; }
alert( msg );
See the idea? Don't try to output the message all in one go. Build it up from pieces.
__________________
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.
another approach that you probably can't use but you might want to look at:
Code:
<script>
var user=true;
var cpuInput = ["rock","paper","scissors"][Math.floor(Math.random()*3)];
var opts={
rock:{rock:"draw",paper:"win",scissors:"lose"},
paper:{rock:"lose",paper:"draw",scissors:"win"},
scissors:{rock:"win",paper:"lose",scissors:"draw"}
}
while(user){
user=prompt("please choose rock, paper or scissors","");
var txt=user?"Computer chose "+cpuInput+", you chose "+user+". You "+opts[cpuInput][user]:"game over";
alert(txt)
}
</script>
If you want to make it more compact and unreadable:
Code:
<script>
var opts={rock:{rock:"tied",paper:"lose",scissors: "win"},
paper:{rock:"win",paper:"tied",scissors: "lose"},
scissors:{rock:"lose",paper:"win",scissors: "tied"}
};
while( true )
{
if ( (u = prompt("please choose rock, paper or scissors",""))=="") break;
alert( opts[u] ? "Computer chose "+(c=["rock","paper","scissors"][Math.floor(Math.random()*3)])
+", you chose "+u+". You "+opts[u][c]
: "invalid choice"
);
}
</script>
(Could have made it even more unreadable, of course, but this seems bad enough.)
That doesn't look unreadable to me. It looks like concise properly written JavaScript - except that in real JavaScript the prompt an alert calls would be replaced by a form and an innerHTML call respectively - which would also do away with the while loop as each "loop" would be triggered by submitting the form.
Ummm...Felgall, where do you see var in that code?
To me, that makes it ugly.
And then I really think that the alert( opts[u] ? ... ); is too long and ugly. I think there is a time and place for the ternary operator, but this place just makes the code ugly. And probably unreadable for novices.
And even if it's not unreadable, it's ugly. My opinion, only.
Anyway, Xelawho and I are just having fun now. I gave an answer I think you'd approve of earlier. in post #8.
__________________
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.
Ummm...Felgall, where do you see var in that code?
In the part that would still be there once you got rid of the prompt and alert.
With what I said needed to be replaced to convert it to real Javascript all that would be left of the code would be the one statement starting var opts = ... and that the rest of the code will have been completely replaced in getting rid of the prompt and alert.