Javascript Codeacademy query - javascript

So i built a rock paper scissors game on codecademy and was tweaking it. The thing that i tweaked is that when the user writes an inappropriate choice, the code stops.
Everything worked but when i write "rock, paper or scissor" it still says inappropriate choice.
var userChoice = prompt("Do you choose rock, paper or scissors?");
//tweaking starts here
if (userChoice!=="rock") {
console.log("inappropriate choice");
}
else if (userChoice!=="paper") {
console.log("inappropriate choice");
}
else if (userChoice!=="scissors") {
console.log("inappropriate choice");
};
// tweaking ends here
var computerChoice = Math.random();
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) {
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);
<!-- end snippet -->

This is a classic if else if ladder problem.
As mentioned in the Java Tutorial here
However, once a condition is satisfied, the appropriate statements are executed [...] and the remaining conditions are not evaluated.
In your case, when you enter "paper" or "scissor", it breaks out at the first branch as userChoice != "rock". If you enter "rock", it breaks out at the second branch as userChoice != "paper".
Ultimately it doesn't make it through the if-else-if ladder and eventually prints the inappropriate choice message.
In order to validate the input such that the user doesn't enter anything but the strings, "rock", "paper" or "scissor", change your tweaking code as follows:
//tweaking starts here
if (userChoice!=="rock" && userChoice!=="paper" && userChoice!=="scissors"){
console.log("inappropriate choice");
}
}; // tweaking ends here
Always remember:
Only one branch of the ladder gets executed...!!! In case multiple
conditions are true, the branch first encountered alone shall be
executed.

Here's an example that might help you see how your if statement logic was incorrect:
var userChoice = prompt("Do you choose rock, paper or scissors?");
if ((userChoice != "rock") && (userChoice != "paper") && (userChoice != "scissors")) {
console.log("inappropriate choice");
} else {
console.log("appropriate choice!");
}
Here's a JSFiddle: https://jsfiddle.net/j66to3x0/2/

Related

Javascript function does not run when called

var gameFunction = function()
{
var userChoice = prompt("What do you choose: rock, paper, or scissors?")
var computerChoice = Math.random();
if (0 < computerChoice < 0.33)
{
computerChoice = "rock";
}
else if (0.33 < computerChoice < 0.67)
{
computerChoice = "scissors";
}
else
{
computerChoice = "paper";
}
console.log("Computer choice: ",computerChoice)
if (userChoice === computerChoice)
{
return "The result is a tie! Enter a new result?"
gameFunction();
}
else if (userChoice === "rock")
{
if (computerChoice === "scissors")
{
return "rock wins"
}
else if (computerChoice === "paper")
{
return "paper wins"
}
}
else if (userChoice === "paper")
{
if (computerChoice === "rock")
{
return "paper wins"
}
else if (computerChoice === "scissors")
{
return "scissors win"
}
}
else if (userChoice === "scissors")
{
if (computerChoice === "paper")
{
return "scissors wins"
}
else if (computerChoice === "rock")
{
return "rock win"
}
}
}
gameFunction();
This is the 9/9 section of the "Rock paper scissors" game from Codecademy: Javascript.
My problem is this:
When the User and Computer ties, it's supposed to re-run the entire "gameFunction" function, meaning it should ask a new input from the user and get a new input from the computer.
However, the program just prints out "The result is a tie!" without re-running "gameFunction." How can I fix this?
No line executed after return statement.. try
gameFunction();
return "The result is a tie! Enter a new result?"
The return statement exits the "gameFunction" function so it doesn't execute the next line. Try using a prompt instead like this:
if (userChoice === computerChoice)
{
prompt("The result is a tie! Enter a new result?");
gameFunction();
}
This way the user can respond to your prompt and you can use it to decide if the game is to continue. You could always just use an alert as well :)
Change return to alert(), like below:
From:
return "The result is a tie! Enter a new result?"
To:
alert("The result is a tie! Enter a new result?");
what about this: https://jsfiddle.net/41gcfL6g/
The thing here is adding a parameter to the function, so you can determine if have been a tie the last time you played. And then in the case of tie, instead of calling the function after the return, you return the result of the gameFunction
The recusive gameFunction() method inside the gameFunction() because the
control returns to the first calling function.
if (userChoice === computerChoice)
{
return "The result is a tie! Enter a new result?"
gameFunction();
}
So instead of return you can print a message there showing there is a tie.
if (userChoice === computerChoice)
{
alert("The result is a tie! Enter a new result?")
gameFunction();
}
and when the above condition is not met then it simply returns to the calling area and stop.
see fiddle, don't use return, console.log https://jsfiddle.net/o62vda05/
var userChoice;
function startGame()
{
userChoice = prompt("What do you choose: rock, paper, or scissors?");
gameFunction();
}
function gameFunction()
{
var computerChoice = Math.random();
if (0 < computerChoice < 0.33)
{
computerChoice = "rock";
}
else if (0.33 < computerChoice < 0.67)
{
computerChoice = "scissors";
}
else
{
computerChoice = "paper";
}
console.log("Computer choice: ",computerChoice)
if (userChoice === computerChoice) {
console.log( "The result is a tie! Enter a new result?");
startGame();
} else if (userChoice === "rock") {
if (computerChoice === "scissors")
{
console.log( "rock wins");
}
else if (computerChoice === "paper")
{
console.log( "paper wins");
}
} else if (userChoice === "paper") {
if (computerChoice === "rock") {
console.log( "paper wins");
} else if (computerChoice === "scissors") {
console.log ("scissors win");
}
} else if (userChoice === "scissors") {
if (computerChoice === "paper") {
console.log ("scissors wins");
} else if (computerChoice === "rock") {
console.log( "rock win");
}
}
}
startGame();

Validate user input string passed into Javascript prompt method

Homework help vampire alert. I'm trying to add a user input string validation function to the "Rock, paper, scissors" game on Codecademy. The text editor on the Codecademy site validates my code as correct, but I'm not getting the expected behavior, which is to return an alert if the user fails to enter either a "rock", "paper", or "scissors" string.
I've piped together the conditions in the if statement below:
var userChoice = prompt("Do you choose rock, paper or scissors?");
function validateUserChoice(userChoice) {
if (userChoice !== "rock" || userChoice !== "paper" || userChoice !== "scissors") {
alert("You can only select rock, paper or scissors!");
return false;
}
};
And here's the rest of the game. When the preceding function is invoked, it appears that the compare function below is bypassed and whatever string the user typed into the prompt is printed to the screen (see console.log("User Choice: " + userChoice); at the bottom). Otherwise, the game returns the expected behavior when the function above is commented out:
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
};
var compare = function(userChoice, computerChoice) {
if (userChoice === computerChoice) {
return "The result is a tie!";
};
if (userChoice === "paper") {
if (computerChoice === "rock") {
return "paper wins";
} else {
if (computerChoice === "scissors") {
return "scissors wins";
}
}
};
if (userChoice === "scissors") {
if (computerChoice === "rock") {
return "rock wins";
} else {
if (computerChoice === "paper") {
return "scissors wins";
}
}
}
};
console.log("User Choice: " + userChoice);
console.log("Computer Choice: " + computerChoice);
compare(userChoice, computerChoice);
Try changing the condition like below,
function validateUserChoice(userChoice) {
if (userChoice !== "rock" && userChoice !== "paper" && userChoice !== "scissors") {
alert("You can only select rock, paper or scissors!");
return false;
}
};

Program works, but Code Academy doesn't recognize it as correct

This is my code:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
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) {
return "The result is a tie!";
}
else if (choice1 === "rock") {
if(choice2 === "scissors") {
return "Rock smashes the fuck out of those scissors!";
}
else {
return "Oh shit, that rock was not prepared for full coverage OF PAPER ";
}
}
else if (choice1 === "paper") {
if (choice2 === "rock"){
return "Oh damn, that paper just covered the shit out of that rock!";
}
else {
return "Those scissors cut that weak paper shit into pieces!";
}
}
else if (choice1 === "scissors") {
if (choice2 === "rock"){
return "Wow! That rock smashed those scissors so hard into the ground that their grandparents felt it. And they're dead!";
}
else {
return "That wimpy-ass paper was sliced into ribbons...Well, you know, if the paper was made of that ribbon material. BUT IT'S NOT.";
}
}
}
compare(computerChoice, userChoice);
The console returns the correct result, the code works, yet CodeAcademy says it's wrong. When I save and submit, the error message says:
Your code returned 'That wimpy-ass paper was sliced into ribbons...Well, you know, if the paper was made of that ribbon material. BUT IT'S NOT.' instead of 'undefined' when the inputs are scissors and paper
What the hell is going on right now?
You need to do everything like CodeAcademy expects you to do. Use the same variables name, the same code structure, etc.

I'm learning Javascript, I'm trying to find out why my "Rock paper scissors" game wont work . .

Ok, so I'm sure this code is FILLED with inaccuracies, but I got really confused when I finished this lesson on the CodeAcademy website and then tried to copy/paste it into my own javascript.html file (where I keep stuff I'm learning) and the code wouldn't work! Then I plugged it in to jsfiddle to play with it and still couldn't get it to work.
here is the original code that I copy/pasted from CodeAcademy:
http://jsfiddle.net/p46pnwvx/
Javascript:
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("Player: " + userChoice);
console.log("Computer: " + computerChoice);
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 win!";
}
}
else if (choise1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
}
else {
return "Rock wins!";
}
}
}
console.log(compare(userChoice, computerChoice));
When this loads, the PROMPT box comes up, but nothing else happens.
Here is what I was playing around with, trying to make the game start and reset with a button, which I thought would be more useful (and educational) than having a game just up and start on it's own.
http://jsfiddle.net/o9ckcy52/
HTML
<h1>Rock, Paper, Scissors Game!</h1>
<button id="start" type="button" onclick="startGame()">Wanna Play?</button>
<p id="choice"></p>
<p id="result"></p>
Javascript
function startGame(){
document.getElementById("start").innerHTML = "Play again?"; // reset's button
return prompt("Do you choose rock, paper or scissors?");
var userChoice = startGame(); // stores result of startGame as user's choice
var computerChoice = Math.random(); // randomly creates computer's choice
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
function compare(choice1, choice2) { //compares user's choice and computer's choice
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 win!";
}
}
else if (choise1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
}
else {
return "Rock wins!";
}
}
}
document.getElementById("choice").innerHTML= // prints user and computer selections
"Player: " + userChoice + /r
"Computer: " + computerChoice;
document.getElementById("result").innerHTML = // prints result of selection comparisons
(compare(userChoice, computerChoice));
}
Here the html works, but nothing happens when you click the button.
Any help given would be appreciated.
I updated your code. There were just some minor issues that were fixed. For example, the userChoice should be assigned to the prompt. Also, your regex /r is invalid and should also be wrapped in quotes, which becomes "\r". You also mispelled one of your choice1's.
Run the snippet below:
function startGame() {
document.getElementById("start").innerHTML = "Play again?"; // reset's button
var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random(); // randomly creates computer's choice
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if (computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
function compare(choice1, choice2) { //compares user's choice and computer's choice
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 win!";
}
} else if (choice1 === "scissors") {
if (choice2 === "paper") {
return "Scissors win!";
} else {
return "Rock wins!";
}
}
}
document.getElementById("choice").innerHTML = // prints user and computer selections
"<pre>" +
"Player: " + userChoice + "\r" +
"Computer: " + computerChoice +
"</pre>";
document.getElementById("result").innerHTML = // prints result of selection comparisons
(compare(userChoice, computerChoice));
}
document.getElementById("start").onclick = startGame;
<h1>Rock, Paper, Scissors Game!</h1>
<button id="start">Wanna Play?</button>
<p id="choice"></p>
<p id="result"></p>
1- Use http://jsbeautifier.org/ to clean up your js code, you are mixing Spaces and Tabs. Also it will clean your code alignment.
2- return prompt("Do you choose rock, paper or scissors?");
will always return before any code get's executed.
3- You are calling startGame() from the button click right? then why you are calling it again from inside the function it self?
Which indicates that you have a missing closing bracket } for the function.
var userChoice = startGame();

Rock paper scissors tie case on JavaScript

I am currently learning from CodeAcademy JS course and they told me to try to implement a retry
case if the choice of the user and the choice of the computer are the same.
Firstly, allow me to apologize for my bad indentation, as I am still learning how to properly ident my code.
Take a look at the if(choice1 === choice2).Under it I have written my way of performing a retry case, but it failed.I would be very glad to recieve help on how should I create a retry case if there is a tie between the choices?
Thanks in advance!
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
userChoice = prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
console.log("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var compare = function (choice1,choice2){
if (choice1 === choice2) {
userChoice = prompt("Choose again");
computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
} else if(computerChoice <= 0.67) {
computerChoice = "paper";
} else {
computerChoice = "scissors";
}
compare(userChoice,computerChoice);
}
if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}
if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}
if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
};
compare(userChoice,computerChoice);
Here you go:
function rockpaperscissors() {
var userChoice = prompt("Do you choose rock, paper or scissors?");
while (userChoice !=="rock" && userChoice !=="paper" && userChoice !=="scissors"){
confirm(userChoice + " isn't rock, paper or scissors. How about you try again?");
userChoice = prompt("Do you choose rock, paper or scissors?");
}
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "rock";
}
else if(computerChoice <= 0.67) {
computerChoice = "paper";
}
else {
computerChoice = "scissors";
}
console.log("You Chose: "+userChoice);
console.log("The Computer Chose: "+computerChoice);
var choice1 = userChoice;
var choice2 = computerChoice;
if (choice1 === choice2){
confirm('Sorry, but there was a tie. You and the computer are equals. Let\'s try again anyways.');
return rockpaperscissors();
}
if (choice1 === "rock"){
if (choice2 === "paper"){
return "Paper wins";
}
if (choice2 === "scissors") {
return "Rock wins";
}
}
if (choice1 === "paper"){
if (choice2 === "scissors"){
return "Scissors wins!";
}
if (choice2 === "rock"){
return "Paper wins!";
}
}
if (choice1 === "scissors"){
if (choice2 === "rock"){
return "Rock wins!";
}
if (choice2 === "paper"){
return "Scissors wins!";
}
}
}
confirm(rockpaperscissors());
Now then, to explain what I did:
Everything now lives within the rockpaperscissors function. This way you can call it from within itself without duplicating code.
Reindented everything with two spaces. Seriously, indentation is important.
If there is a tie, it'll confirm you telling you that there's a tie, followed by restarting itself.
Once a not-tie has been reached, it will confirm you the result.
Additionally, because I felt like it, a very refined version that uses lots of complicated JS:
function rockpaperscissors() {
var user = '';
do {
user = prompt((user!==''?'Invalid answer. ':'')+'Choose rock, paper, or scissors.');
} while (['rock', 'paper', 'scissors'].indexOf(user) == -1);
var computer = ['rock', 'paper', 'scissors'][Math.floor(Math.random()*3)];
switch (user+'|'+computer) {
case "rock|scissors":
case "paper|rock":
case "scissors|paper":
return "User Wins!";
case "rock|rock":
case "paper|paper":
case "scissors|scissors":
return rockpaperscissors();
default:
return "Computer Wins!";
}
}
confirm(rockpaperscissors());
I'm not too clear on how your program is running but what you could do is break up you code into different functions. For example, have a function just to handle moves, another one that handles game logic (the score, looking for fouls, etc...)
If you had a function that would return the computers move, you would just use your same logic to say if (choice === computerMove) However, I would not compare computerMove with whatever the user types in with the identity comparison because you never know if the user will misspell rock or something. You could have this 'game logic' function return maybe a value that would tell if there is a winner, if both players made the same move (foul), or which turn it is.
If you're really hurting for a code snippet, let me know and I'll create one.

Categories

Resources