9/9 game returns a tie question


#1

What I did in the beginning was to just do that:
if (choice1 === choice2) {

return prompt("play again");
}

it didn't really helped and i found a code(couldn't comment there since it was achieved) and the entire code was wrapped in a function but i don't understand why. Can someone explain?
Here the code (the changes regarding the returns a tie question are in red)
play=function() {
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) {
confirm("Tie! go again!")
play()
}
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"
}
}
else {
return "sorry, that's not an option!"
}
}
compare(userChoice,computerChoice)
}
play()

Thank you


#2

First question. Did it work? Cause if it worked that's fine.

The reason the member probably put it in a function was to get the function to work. That's not to say it shouldn't work without the function, but the function provides reusability. Say you wanted to put this in another program then you could just call the function over and over again as needed.

Otherwise there is no necessity to putting it into a function then calling the function. Perhaps it's simply user choice.


#3

When I only had :
if (choice1 === choice2) {

return prompt("play again");
}

and choice1 was = choice2, the window came up and said "play again". However, when I typed in again for example rock, it wouldn't give me a result (who won) it would just leave that: Computer: rock
rock