Creating a way to rerun program for when the result is a tie


#1


Hi everyone! I just finished building the simple Rock, Paper, Scissor game, but wanted to expand on the code to see if I could get it to rerun itself if the result of the first outcome is a tie.
What I wanted to do, instead of just making the code a lot longer, is to make the entire game a function, and then have the program rerun that function if the initial result is a tie.

Now I keep coming back with syntax errors, and I'm not entirely sure why.

Please correct me if my logic is wrong (and please help me understand why that is the case) and/or help me debug this code? Thank you!


var game = function(start) {

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 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!";
        }
     }
}
    
}

var result = compare(userChoice, computerChoice);
console.log(result)

if (result === "The result is a tie!") {
    result = game(start);
}
else {
    return result;
}

#2

here:

var result = compare(userChoice, computerChoice);

you can't. You will need to call the the game function first: (since the compare is inside the game function):

var result = game("a parameter is required");

then you can call your compare function inside of the game function:

else if (choice1 === "scissors") {
        if (choice2 === "rock"){
        return "Rock wins!";
        }
        else {
            return "Scissors wins!";
        }
     }
}
    result = compare(userChoice, computerChoice);
    return result;
}

Or something like that. Not sure, but this should get you in the right direction