This forum is now read-only. Please use our new forums! Go to forums
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
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!
Answer 55c63754d3292f1bb10002ae
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.
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
i HAVEN’T FINISHED IT YET.. STILL WORKING ON THE RESULTS TO BE MORE INTERACTIVE.. ENJOY! :)