9. How do I re-enter new value when there is a tie?


#1


Hi I am able to re-enter a new value rock,paper or scissors when there is a tie but it returns with a "undefine" how do I re-do random math thing do I need to put it into another reDo (); thing? I'm very n00bish sorry I'm trying!



user: paper
Computer: paper
undefined


var 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 = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);


var compare = function(choice1,choice2)
{
    
    if (choice1 === choice2 )
    {
// return "The result is a tie!" ;   
prompt("Sorry your result is a tie, please make a new choice?");
    }
    
    

// if they choose rock
else if (choice1 === "rock"){
if (choice2 === "scissors"){
return "rock wins";  
}
else {
return "paper wins";   
}
}

 // if they choose paper
else if (choice1==="paper") {
if (choice2==="rock") {
return "rock wins";
}
else {
return "scissors win";
}
}
 
else if (choice1 === "scissors")
{
 if (choice2 === "rock"){
 return "rock wins"
 }
 else {
  return "scissors wins"   
 }
}
 
 
}


console.log(compare (userChoice,computerChoice));


#2

You know how to make functions, you could make a function which will get you userChoice and computerChoice, so that you can call this function when you want the user to choice again


#3

Sorry I have no idea what that means. DO I do something like var Tie = Function() { } ???


#4

no, you need to make a function:

var choices = function(){
   // user choice and computer choice
}

so you can call this function whenever you want the user and computer to pick a choice.


#5

That's exactly what I wrote but I used Tie instead of choices haha :joy:
I will have another go I'm still a bit confused would I need to take any code out of the existing to put into the new function or just rewrite it in there?
Thank you :slight_smile:


#6

well, this is where you pick the choices:

var 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 = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);

so that would be good content for choices function


#7

Hello I'm not sure what Im doing wrong but it comes up with undefined again if it is also a tie on the 2nd time of choosing.

var 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 = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);



var tie = function(){
 prompt("Sorry your result is a tie, please make a new choice?");
 
if (userChoice === "rock") {
        userChoice = "rock";
}

else if (userChoice === "scissors") {
    userChoice = "scissors";
}

else if (userChoice === "paper") {
    userChoice = "paper";
}


else {
    (userChoice = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);


}

// END 

var compare = function(choice1,choice2)
{
    
    if (choice1 === choice2 )
    {
// return "The result is a tie!" ;   
tie();
    }
    
    

// if they choose rock
else if (choice1 === "rock"){
if (choice2 === "scissors"){
return "rock wins";  
}
else {
return "paper wins";   
}
}

 // if they choose paper
else if (choice1==="paper") {
if (choice2==="rock") {
return "rock wins";
}
else {
return "scissors win";
}
}
 
else if (choice1 === "scissors")
{
 if (choice2 === "rock"){
 return "rock wins"
 }
 else {
  return "scissors wins"   
 }
}
 
 
}


console.log(compare (userChoice,computerChoice));

Thanks!


#8

okay, here you go:

var 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 = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);



var tie = function(){
 userChoice = prompt("Sorry your result is a tie, please make a new choice?");
 
if (userChoice === "rock") {
        userChoice = "rock";
}

else if (userChoice === "scissors") {
    userChoice = "scissors";
}

else if (userChoice === "paper") {
    userChoice = "paper";
}


else {
userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}


console.log("user: " + 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);
  return compare(userChoice, computerChoice)

}; // END of tie

var compare = function(choice1,choice2)
{
    
    if (choice1 === choice2 )
    {
// return "The result is a tie!" ;   
return tie();
    }
    
    

// if they choose rock
else if (choice1 === "rock"){
if (choice2 === "scissors"){
return "rock wins";  
}
else {
return "paper wins";   
}
}

 // if they choose paper
else if (choice1==="paper") {
if (choice2==="rock") {
return "rock wins";
}
else {
return "scissors win";
}
}
 
else if (choice1 === "scissors")
{
 if (choice2 === "rock"){
 return "rock wins"
 }
 else {
  return "scissors wins"   
 }
}
 
 
}


console.log(compare (userChoice,computerChoice));

when the result is a tie, you have to call compare again. (also corrected some mistakes in there), you might also want to log that the result is a tie:

if (choice1 === choice2 )
    {
// log that result is a tie here
// return "The result is a tie!" ;   
return tie();
    }

#9

Ohhh thats why it wasn't showing me the end result. Thanks you so much! Sorry for spoon feeding this one to me !!


#10

Thats how I did it.

var 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 = "Didn't pick rock, paper or scissors");
var userChoice = prompt("Sorry you didn't pick rock, paper or scissors please choose again.");
}
console.log("user: " + userChoice);

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

function compare(userChoice,computerChoice) {

if (userChoice === computerChoice) {
    return "The result is a tie!";   
}

else if(userChoice === "rock") {
if(computerChoice==="scissors") {
return "rock wins"
}
else {
return "paper wins";
}
}

else if(userChoice === "paper") {
if(computerChoice==="rock") {
return "paper wins"
}
else {
return "scissors wins"
}

}

else if(userChoice === "scissors") {
if(computerChoice==="rock") {
return "rock wins"
}
else {
return "scissors wins"
}
}

}
console.log(compare (userChoice,computerChoice));


#11

None of this makes any sense. Why not read and follow the instructions, to the letter, rather than embellishing the program with code that is not asked for?

Read the instructions. The compare function is to have two parameters, choice1 and choice2. The other variables are globals and do not belong inside the compare function.

var compare = function (choice1, choice2) {
    // if statement code
};

Complicating the exercise by adding input validation just muddies the waters. Get the actual exercise right, first, then play with it on your own without bringing the added problems to the Q&A forum. That is what the Corner Bar is for.


#12

I thought that 1 satisfied the task of only allowing rock,paper or scissors and nothing else? I found that on the forums and it works when you run it. Is there a correct way of doing this?


#13

@mtf, the user is at 9. next steps which means he finished the track, and now tries to implement one of the ideas/suggestion of exercise 9.

@lovebexa, ideally you would use a do while loop but this hasn't been covered yet. You can use function to just keep prompting endlessly until the user enters a right choice.

I have to agree with mtf that this:

    if (userChoice === "rock") { userChoice = "rock";}

    else if (userChoice === "scissors") { userChoice = "scissors";}

    else if (userChoice === "paper") { userChoice = "paper";}

is pretty pointless. If the user picked one of the valid choices, userChoice already contains rock, paper or scissors, so i don't see why you should set it again


#14

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