How to define the results of the returns so I can see... the results of the game


#1

I don't know if I'm in the wrong place but now that i'm here, I'd rather hurry up and get this off my chest and be SEEN, even if it's just to tell me to go away.

This is the rock paper problem. I'm trying to make my returns a result variable that will allow me to print them to the console.

All help is welcome.

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("User: " + 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 wins";
        }
    }
    else if(choice1 === "scissors") {
        if(choice2 === "rock") {
           return "rock wins";
        }
        else {
            return "scissors wins";
            console.log("scissors wins");
        }
    }
var theResult = compare(userChoice, computerChoice);
console.log( theResult )

#2

According to this post and the post in the other thread I'd say that you're code isn't that off. The problem is just that you call the function inside itself. But as functions are only executed when a start command is given (function call) and this start command is in the function itself, well then they're only executed when you give another start command outside of the function. And you might get into a recursion of calling the function inside the function aso... Anyway all you need to do is move the function call after the last } of your function. BTW this code seem to lack the last one (probably just due to copy and paste as it would have created an error otherwise).

Also this:

        else {
            return "scissors wins";
            console.log("scissors wins"); // this
        }

doesn't make this much sense because return leaves the function so the console.log coming after it will just be ignored.

And last but not least the reason why this passes. Well most likely what the test function does is calling your compare function with valid inputs and watch whether or not it gets the expected outputs and as your function serves the purpose and the critical function call inside the function is at a point which will never be reached for valid inputs you're fine and are allowed to pass on to the next exercise.


#3

THANK YOU SO MUCH.
it's been about 3 hours i've been working on this, "debugging" to no avail. It feels so amazing to see paper wins printed to the console.

Thank you thank you thank you ^.^