Rerun 9. Next Steps


#1



https://www.codecademy.com/courses/javascript-beginner-en-Bthev-mskY8/1/5?curriculum_id=506324b3a7dffd00020bf661


Computer: rock
Do you wish to play again?

As you see it skipped the comparison and printed the next line, any help why is it doing so?
Also I don't really get how do I repeat the game again.


var userChoice = prompt("Do you choose rock, paper,scissors or rope?");
var computerChoice = Math.random();
if (computerChoice < 0.25) {
	computerChoice = "rock";
} else if(computerChoice <= 0.50) {
	computerChoice = "paper";
} else if(computerChoice <= 0.75) {
	computerChoice = "scissors";
} else {
    computerChoice = "rope";
    }
console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2)
{
    if(choice1 === choice2)
    {
        return ("The result is a tie!");

    }
    
    else if (choice1 === "rock")
    {
        if (choice2 === "scissors")
        {
            return ("rock wins");   

        }
        
        else if (choice2 === "paper")
        {
            return ("paper wins");    

        }
        
        else
        {
            return ("rope wins");    
 
        }
    }
    else if (choice1 === "paper")
    {
        if (choice2 === "rock")
        {
            return ("paper wins");    

        }
        
        else if (choice2 === "scissors")
        {
            return ("scissors wins");

        }
        
        else 
        {
            return ("rope wins");    

        }
    }
    
    else if (choice1 === "scissors")
    {
        if(choice2 === "rock")
        {
            return ("rock wins");    

        }
        else if (choice2 === "paper")
        {
            return ("scissors wins"); 

        }
        else
        {
            return ("scissors wins");    

        }
    }
    
    else if (choice1 === "rope")
    {
        if(choice2 === "rock")
        {
            return ("rope wins");    

        }
        
        else if (choice2 === "paper")
        {
            return ("rope wins");    

        }
        
        else
        {
            return ("scissors wins");    

        }
    }
    
    else
    {
        console.log("Please enter Rock, Paper,Scissors or Rope only.");   

    }
};

compare(userChoice, computerChoice);
console.log("Do you wish to play again?");


#2

@arrayninja43363,

What you want is a loop-construct...
create the getUserChoice function using return-statement
create the getComputerChoice function using return-statement
creat the compare function using return-statement ( allready done !!)

As you are using return-statements you call the compare-function
var theResult = compare(userChoice,computerChoice);
and thus having the =result= of the game in the theResult variable

===========================================

var getUserChoice = function () {
 var userChoice;
 do {
      userChoice = prompt("Choose either rock, paper or scissors","paper");
     } while (userChoice !== "rock" &&
          userChoice !== "paper" &&
          userChoice !== "scissors");
     // console.log("UserChoice is 1: "+ userChoice);
     return userChoice;
};
var getComputerChoice = function() {
var computerChoice = Math.random();
  if (computerChoice < 0.34) {
    computerChoice = "rock";
  } else if(computerChoice <= 0.67) {
	computerChoice = "paper";
  } else {
	computerChoice = "scissors";
  }
  //console.log("computerChoice 1: " + computerChoice);
  return computerChoice;
};

//var userChoice = getUserChoice();
//var computerChoice = getComputerChoice();

//=================================

var compare = function(choice1, choice2){ ///opens the function
  if(choice1 === choice2){ ///opens the if
      return("The result is a tie!");
  } ///closes the if

  else if (choice1 === "rock"){ ///opens the else if
      if(choice2 === "scissors"){ ///opens the if
          return "rock wins";
      }  ///closes the if
      else{ ///opens the else
          return "paper wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "paper") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return "paper wins";
      } ///closes the if
      else { ///opens the else
          return "scissors wins";
      } ///closes the else
  } ///closes the else if

  else if (choice1 === "scissors") { ///opens the else if
      if (choice2 === "rock") { ///opens the if
          return("rock wins");
      } ///closes the if
      else { ///opens the else
          return("scissors wins");
      } ///closes the else
  } ///closes the else if

}; ///closes the function

==============================================================

var playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";
    while (playAgain) {
    //execute the pre-declared getUserChoice-function
    userChoice = getUserChoice();
    console.log("User choice is " + userChoice);

```
        //execute the pre-declared getComputerChoice-function
        computerChoice = getComputerChoice();
        console.log("Computer choice is " + computerChoice);

        //execute the compare-function and capture the-result
        theResult = compare(userChoice,computerChoice);
        console.log("The result is " + theResult);

        //if (theResult === "It is a tie!") {
        //    console.log("It is a tie!");
        if (theResult === "The result is a tie!") {
            //console.log("The result is a tie!");
            playAgain = true;
            console.log("==== Play once more =====");
        } else {
            playAgain = false;
            console.log("==== End of Game =====");
        }

    //end of -while- loop is reached, and if VALUE of playAgain === true
    // the loop will start over
    } //closing the while-loop

}; //closing the function-body of playTheGame

##======================================================================

// call the function playTheGame
playTheGame();

#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.