Marin Marin - 5 months ago 8
Javascript Question

Code is not giving any return string



var userChoice = prompt("What would you like to play?"),
computerChoice = Math.random();
if (computerChoice <= 0.34){
computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
computerChoice = "Paper";
}
else {
computerChoice = "Scissors";
}
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
if (choice1 === choice2){
return "This 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";
}
}
};

document.write(compare(userChoice, computerChoice));

<!doctype html>
<html>
<title>JS Practice</title>
<head>
<script language="javascript">
</script>
</head>
<body>



</body>

</html>





Help me please to understand why it doesn't show return's strings?? When you win or lose it supposed to show the result. I got this code from codeacademy. On their console in works perfectly, but when you try to run it on browser it does not work properly. Confused...



var userChoice = prompt("What would you like to play?");
var computerChoice = Math.random();
if (computerChoice <= 0.34){
computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
computerChoice = "Paper";
}
else {
computerChoice = "Scissors";
}
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2){
if (choice1 === choice2){
return "This 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);




Answer

The code is correct and working properly, you just forgot the important part of showing it.

You aren't writing the result to the actual document. You call the function and it returns properly, but you aren't showing the answer visibly. Try this code:

var userChoice = prompt("What would you like to play?"),
    computerChoice = Math.random();
if (computerChoice <= 0.34){
    computerChoice = "Rock";
}
else if (computerChoice >= 0.35 && computerChoice <= 0.67){
		computerChoice = "Paper";
}
else {
	computerChoice = "Scissors";
}	
document.write("Computer's choice is " + computerChoice);
var compare = function(choice1, choice2) {
	if (choice1 === choice2){
		return "This 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";
		}
	} 
    else {
        return "Invalid input! Type either rock, paper, or scissors!"
    }
};

document.write(", " + compare(userChoice, computerChoice));

You were missing the last document.write to write the result to the document. You can format it yourself. I'd also suggest adding another case if the input isn't valid. You could also put the main prompt in a function and call recursion if the input isn't valid.