"Next Steps" - attempt to code for 'picking again'


#1



I'm stuck on the 'extra' parts of the last exercise. Note that I've changed the first computer input to be 'rock' so that I can force the tie, so as to test the 'pick again' scenario.

The code runs, and you and the computer select new choices. But then, the 'win' messages are not displayed.
This confuses me because if there is a second tie, the first part of the function runs again (it prompts me to pick again). But if there isn't, it no longer displays the 'win' messages (I always pick rock, to force the first 'if' set. Any ideas?

I assume the problem is that you cannot call the function within itself. Would I just have to create two identical functions and call the second one inside the first that runs, in the case of a tie? And then recursively call the other?

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = "rock";
/* 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) {
        userChoice = prompt("Pick Again!");
        computerChoice = Math.random();
        if (computerChoice < 0.34) {
        	computerChoice = "rock";
        }
        else if(computerChoice <= 0.67) {
        	computerChoice = "paper";
        }
        else {
        	computerChoice = "scissors";
        }
        console.log("Computer: I'll try again: " + computerChoice);
        compare(userChoice, computerChoice);
    }
    else if (choice1 === "rock") {
        if (choice2 === "scissors") {
            return "rock wins";
            console.log("rocky");
        }
        else {
            return "paper wins";
            console.log("papery");
        }
    }
    else if (choice1 === "paper") {
        if (choice2 === "rock") {
            return "paper wins";
        }
        else {
            return "scissors wins";
        }
    }
    else if (choice1 === "scissors") {
        if (choice2 === "paper") {
            return "scissors wins";
        }
        else {
            return "rock wins";
        }
    }
    else {
        return "you can't spell"
    }
}

compare(userChoice, computerChoice)


#2

I assume the problem is that you cannot call the function within itself.
Would I just have to create two identical functions and call the second
one inside the first that runs, in the case of a tie? And then
recursively call the other?

Obama says one time : Yes you can !

The main problem is that your function need to return something. So when you call again your function inside the main one, you have to return the result of your call. Like this :

return compare(userChoice, computerChoice);

But, I'd liked to say a last thing. You can improve your code if you don't forget semicolons, return statement (each part of the if/else must have one, otherwise a default must have been set) and check the bible of your language.


#3

Thanks Obama

I added the return statement, and now the game works swimmingly


#4

So I modified my code so that if you tie the first time it will ask the player for a new input. But if you tie a second time, it never prompts the user again. How would you add that into the code?


#5

Sorry for the delay!

You need to call the function again (in mine, it is called compare) and feed it the new inputs that the user gives in the result of the tie.

var compare = function (in1, in2){
      if (tie)
          new inputs
          compare(new input, new input)
      else
          regular game code
}