Rock, paper & scissors: Next step


#1

Hello
I'm trying to complete the extra challenges in the rock paper scissors game.
1 what if a user makes an inappropriate choice
3 how to continue the game after a tie.

I’m wondering if its possible to add these into the game with the knowledge Ive learnt so far just by using functions and variables. I think I'll need to use a looping function like while.

My attempts so far was to include a userChoice validating function and to edit the compare function to request a new user choice.
Here's the code.

var userChoice = prompt("Do you choose rock, paper or scissors?");

var validateUserChoice = function (choice0) {
    if (choice0 !== "rock") {
        if (choice0 !== "paper") {
            if (choice0 !== "scissors") {
                userChoice = prompt("Choose either rock, paper or scissors") 
            }
        }
    }
};
validateUserChoice (userChoice);
validateUserChoice (userChoice);


var computerChoice = Math.random();
if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} 
console.log("Computer: " + computerChoice);
console.log("User: " + userChoice);

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";
        }
    }
        
};


compare (userChoice, computerChoice);

#2

does this function work:

var validateUserChoice = function (choice0) {
    if (choice0 !== "rock") {
        if (choice0 !== "paper") {
            if (choice0 !== "scissors") {
                userChoice = prompt("Choose either rock, paper or scissors") 
            }
        }
    }
};

if the user now enters the wrong choice 3 times, you function won't cover it, use a loop:

var validateUserChoice = function (choice0) {
  do {
    userChoice = prompt("Choose either rock, paper or scissors");
  } while (choice0 !== "rock" || choice0 !== "paper" || choice0 !== "scissors");
}

this loop will execute at least once (do), then after the loop is executed it checks if the condition is right (while), if the condition is met (the user has valid input), the loops end, otherwise the loop will run as long the user gives wrong input. You could use a similair loop to keep the game running.

Wait, have you done loops in this stage of the track?


#3

Hi stetim94, thanks for the reply, Ive just started loops. hopefully I'll understand your loop solution. This is the first time seeing the do{ } function.

So I'm guessing its not possible to add these features in to the game just using variables and function.


#4

the do while loop as it is called, will be explained shortly. You could use goto. But i hope you will see that this can get messy/bad readable very quickly. Goto is not a loop (i think), so yes, you could implant it with goto, but loops are easier

As you can read, do not use the goto function. I just wanted to pointed out it exist, but do not use it


#5

Rather wait for loops and don't encourage the use of goto :smile: It might seem tempting because it does what loops do and loops are cool but there is no better way to create spaghetti code (a code so entangled that it is hard to follow at least one string).


#6

i do not encourage the use of goto, but the question was if you can do this without loops. The answer is yes. I think he deservers to know.


#7

Yes you can I've as well seen some tries using recursions and some of them even work still it's rather hacking then coding and might only encourage bad habits :smile: Not meant that you encourage the use of them just that learning about them first might do so.


#8

Then what should i have said? Yes, you can do this without loops. the end? Or say it is not possible. (that would make it a lie)


#9

That's actually a good question. Personally I tell the people asking to proceed with loops and to review this later. But anyway if I'd introduce a new feature it would rather be while then goto.
That is kind of a conceptual bug to have this exercise before having loops.


#10

This exercise works fine, but yea, it makes it so much easier knowing loops so you can repeat something over and over (loop) until user input is good/to repeat the game.

Good point, i will think more about something before including something advanced like goto.


#11

3 posts were split to a new topic: Rock paper scissors next step


#14

13 posts were split to a new topic: Rock paper scissors next step