This forum is now read-only. Please use our new forums! Go to forums

banner
Close banner
0 points
Submitted by IYANDA SAMSON
over 8 years

What if user made an inappropriate choice palaver?

hi guys, kudos! it’s not really easy getting here. i think we all deserve a pat on the back…. but what if a user made an inappropriate choice? i had added these codes but i think the code still runs to some extent. i think there’s should be a way that particular user in question, should be stopped from advancing. ..try this code first and see what i’m talking about.

userChoice = prompt(“Do you choose rock, paper or scissors?”); if (userChoice === “rock”) { userChoice = “rock”; }else if (userChoice === “scissors”) { userChoice = “scissors”; } else if (userChoice === “paper”) { userChoice = “paper”; } else { userChoice = prompt(“Wrong choice, choose one of these options ‘paper, rock or scissors.”) }

…………………………………………………. after this prompt “wrong choice, choose blah blah…., a user might still make another inappropriate choice yet the code run, it probably gives you the computer choice.. why all that? can we simply truncate the process or always prompt “do choose rock, paper or scissors.?

Answer 55c4dc649113cbb045000505

0 votes

Permalink

HELLO.. I MADE A MORE INTERACTIVE ROCK, PAPER OR SCISSORS GAME..UNFORTUNATELY, I CANNOT PASTE IT HERE.. MY CODE IS WAY TOO LONG, IT EXCEEDS THE LIMITS.. BUT HERE’S THE LINK http://pastebin.com/WJ5q8MdH TRY TO COPY, AND PASTE IT IN THE CONSOLE AND PRESS ENTER!

points
Submitted by Yehosua Selah
over 8 years

1 comments

Yehosua Selah over 8 years

i HAVEN’T FINISHED IT YET.. STILL WORKING ON THE RESULTS TO BE MORE INTERACTIVE.. ENJOY! :)

Answer 55c63754d3292f1bb10002ae

0 votes

Permalink

Hi! You don’t need to check it that way, remember the lesson says, if you have to type something repeatedly you must most probably use a function?

so here you can create a function choice() inside which you can put the input and this verification part.

like this:-

 var choice = function()
        {
                var userChoice = prompt("Select rock, paper, scissors,rope");
                if(userChoice!="rock" && userChoice!="paper" && userChoice!="scissors" && userChoice!="rope")
                {
                    alert("You have entered wrong choice, enter rock, paper, scissors or rope");
                    choice();
                }
};

Now notice the “choice();” at the end? It calls the choice function again and again as many times till the user inputs the right choice! Hope this helps!

BTW it’s called a recursion, calling the function inside itself.

points
Submitted by Chirag Shinde
over 8 years