Adding code for user choice other than rock, paper, scissors


#1


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


How would we code for continuous times that the user inputted the wrong value? i.e. that we check / validate the user entered the correct value before proceeding with the compare function. I guess we have not learnt it up until this point in the course - but it does suggest that we have acquired the skills to be able to do so...


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 if (choice1 !== "rock") {
        prompt("You can only choose rock, paper and scissors");
        // only corrects the user once...
    }
}

compare(userChoice, computerChoice);


#2

you could build another function to validate user input before moving on to the compare function?


#3

Thanks for the reply. Only I don't think we have been given the tools to do that until this point...
I do not remember learning how this is done.
I was curious as to the method of accomplishing the task given the principles and directives up until this point on the course. I am experiencing the same thing when the first comparison is a tie - the program runs again to ask the user for another input, however this is only done once.

Thanks.


#4

If you can test whether the input valid, and if you can write a loop, then that's about all you need. Repeat until valid.


#5

you have been given the tools, but tools require experience to work with.

for example, to validate input:

var validate_input = function(){
    userChoice = prompt("make your choice: ");
    /* validate the user choice, if valid, return userChoice */
    /* you can code this */
    /* invalid choice, the function need to run again */
    return validate_input()
}

but @ionatan, loops haven't been taught up to this point in the course


#6

Awesome. So something like this should work:

var validate_input = function() {
userChoice = prompt("make your choice: ");
/* validate the user choice, if valid, return userChoice */
if (userChoice === "rock") {
return userChoice;
} else if (userChoice === "paper") {
return userChoice;
} else if (userChoice === "scissors") {
return userChoice;
} else {
/* invalid choice, the function need to run again */
return validate_input();
}
}

validate_input();

And I can try and do the same for when the game is a tie...
Thanks.


#7

so close, your function returns userChoice, so we need to store this:

userChoice = validate_input();

when you call the function, store the result in userChoice, now the returned result is stored in userChoice


#8

Thanks
When I add this line of code after the line validate_input();
the game runs an extra time. i.e. if I input 'dog' in the pop up twice, then the pop up window will appear a third time instead of going to output the result.
When I just have the validate_input();
line without the userChoice = validate_input();
line then to seems to run as expected.

Perhaps I am not understanding something...


#9

i ran your code in this bin, and everything is fine?


#10

ok - awesome.
Thanks for the tips and help!


#11

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