Word appear in wrong order when using console.log("" + " wasn't an option" + choice1);


#1

Hi everyone,

I'm having trouble when I am trying to extend the function for Lesson 9 "Next Steps".

I am trying to create a response where if the user enters an invalid response (i.e. not paper rock or scissors) then it prints " choice 1 wasn't an option", where choice1 is whatever the user entered.

I'm using the following syntax to try to do this: console.log("" + " wasn't an option" + choice1);

But when I call the function and input dog, for example, the prints wasn't an optiondog.

The full code is below. Any help is much appreciated.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
} 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
{
return "paper wins";
}
}
else if (choice1 === "paper")
{
if (choice2 === "rock")
{
return "paper wins";
}
else
{
return "scissors wins";
}
}
else if (choice1 === "scissors")
{
if (choice2 === "rock")
{
return "rock wins";
}
else
{
return "scissors wins";
}
}
else
{
console.log("" + " wasn't an option" + choice1);
}
};

compare (userChoice, computerChoice);

#2

@t_slaytor,
Don't make changes in the compare function
as the functionality of the compare function
is to return a string Value
when given the argument's userChoice and computerChoice.
( therefor use )
console.log( compare(userChoice,computerChoice) );

It would be better to define a getUserChoice-function
like

var getUserChoice() {
     var theChoice = prompt("Do you choose rock, paper or scissors?");
     //
     //build a construct to test the validity of =theChoice=
     //
     return theChoice
}

and then
var userChoice = getUserChoice()


#3

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

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 playTheGame =function() {
    var playAgain =true;
    var userChoice="";
    var computerChoice="";
    var theResult="";

   while (playAgain) {

       //execute the pre-declared getUserChoice-function
       var userChoice = getUserChoice();
       console.log("User choice is " + userChoice);

        //execute the pre-declared getComputerChoice-function
        var 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!") {
       playAgain = true;
    } else {
      playAgain = false;
    }

    //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();