2
\$\begingroup\$

I built this as part of completing the Codecademy JavaScript lesson. I wanted to perfect it, make it readable and functional, and as efficient as possible. As far as I can tell it can't be any more efficient than this. But maybe I can't tell. What do you think? I know there are little things, maybe the victory variable should have a console.log in it. Probably one or two more. But hopefully this is a good example for people who need help with writing their Rock, Paper, Scissors challenge.

//Loop ensures only valid user input is rock, paper, scissors
var i, infiniteloop;
for (i=0; i<1; infiniteloop++) //infiniteloop allows loop to continue if user is infinitely retarded
{
    var userChoice = prompt("You're playing rock, paper, scissors against the computer. Type your choice in lower case letters.")
    if (userChoice === "rock") {i=1;} //escapes loop
        else if (userChoice === "paper") {i=1;} //escapes loop
        else if (userChoice === "scissors") {i=1;} //escapes loop
        else {alert("Try again, stupid.");} //alerts, loops back to start
}

console.log("You chose " + userChoice + "!"); //announces userChoice

//generate a random computer choice, assign a string to replace it
var computerChoice = Math.random(); //random function. generates a number between 0 and 1
if (computerChoice <= .33) { computerChoice = "rock"; }
    else if (computerChoice <= .66) { computerChoice = "paper"; }
    else { computerChoice = "scissors"; }

console.log("Computer chose " + computerChoice + "!"); //announces computerChoice

var victory = "You win! A master strategist is you." //preset victory announcement
if (userChoice === computerChoice) { console.log("Tie game!"); }
    else if (userChoice === "rock" && computerChoice === "scissors") { victory }
    else if (userChoice === "paper" && computerChoice === "rock") { victory }
    else if (userChoice === "scissors" && computerChoice === "paper") { victory }
    else {console.log("You have suffered a devastating loss.");}
\$\endgroup\$

migrated from stackoverflow.com Jan 4 '16 at 18:21

This question came from our site for professional and enthusiast programmers.

1
\$\begingroup\$

Here are my 2 cents to improve your code.

First of all, if you're going to be using an infinite loop anyways to check for user input, why not make it a while loop instead of a for loop?

while (i === 0)
{
    var userChoice = prompt("You're playing rock, paper, scissors against the computer. Type your choice in lower case letters.")
    if (userChoice === "rock") {i=1;} //escapes loop
        else if (userChoice === "paper") {i=1;} //escapes loop
        else if (userChoice === "scissors") {i=1;} //escapes loop
        else {alert("Try again, stupid.");} //alerts, loops back to start
}

I personally feel that a while loop is far easier to read than a for loop, and in your case you have two variables which doesn't seem necessary. By using a while loop, you won't need to have the 'infiniteloop' variable.

Also, it looks like the victory messages won't be printed to the console as you're just calling the strings after each case. This should do the trick:

else if (...) { console.log(victory); }

Congrats on finishing a game though! It's always a great place to start :)

Best of luck for your coding journey!!

shifubear

\$\endgroup\$
1
\$\begingroup\$

I think that your code could be improved in both readability and simplicity. An example that you could consider.

var choices = ['rock', 'paper', 'scissors'];
var computerChoice = choices[Math.floor(Math.random() * 3)];
var userChoice;
do {
  userChoice = prompt('Type your choice in lower case letters: ' + choices);
} while (choices.indexOf(userChoice) < 0);
var result = 'You: "' + userChoice + '", Computer: "' + computerChoice + '". ';
if (userChoice === computerChoice) {
  result += 'Tie game!';
} else {
  var victory = "You win! A master strategist is you.";
  if (userChoice === "rock" && computerChoice === "scissors") {
    result += victory;
  } else if (userChoice === "paper" && computerChoice === "rock") {
    result += victory;
  } else if (userChoice === "scissors" && computerChoice === "paper") {
    result += victory;
  } else {
    result += 'You have suffered a devastating loss.';
  }
}
document.getElementById('out').textContent = result + '\n';
console.log(result);
<pre id="out"></pre>

\$\endgroup\$
0
\$\begingroup\$

I would propose to check user input this way:

var userChoice = prompt("Do you choose rock, paper, well or scissors?");
while (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors" && userChoice !== "well"){
  var userChoice = prompt("Please, make the correct choice: rock, paper, well or scissors?");
}

\$\endgroup\$
  • \$\begingroup\$ What is "well" doing there? \$\endgroup\$ – janos Jan 21 '17 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy