The console only states what the computer chose


#1

Hey, I have finished the "Build "Rock, Paper, Scissors" course but the console only shows what the pc chosed, it does not display the "The result is a tie!" statement for example. Can someone help me please?


#2

Have you called the function? Anyway it would really help if you could post the code otherwise we could just guess.


#3

var userChoice = prompt("Alegi piatra, hartie sau foarfeca?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "piatra";
} else if(computerChoice <= 0.67) {
computerChoice = "hartie";
} else {
computerChoice = "foarfeca";
} console.log("Computer: " + computerChoice);
    var compare= function(choice1, choice2){
        if(choice1===choice2){
            return"The result is a tie!";
            }
        if(choice1 === "piatra")
            {
                if( (choice2 === "foarfeca")){

                    return( "piatra castiga");}
                else {var userChoice = prompt("Do you choose rock, hartie or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "piatra";
} else if(computerChoice <= 0.67) {
	computerChoice = "hartie";
} else {
 	computerChoice = "foarfeca";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
    return "The result is a tie!"
} else if ( choice1 === "piatra") {
    if (choice2 === "foarfeca") {
        return "piatra castiga"
    }
    return "hartie castiga"
 } else if (choice1 === "paper") {
    if (choice2 === "piatra"){
        return "hartie castiga"
        }
        else {
        return "foarfeca castiga"
    }
}  else if (choice1 === "foarfeca") {
    if (choice2 === "piatra"){
         return "piatra castiga"
        }
        else {
        return "foarfeca castiga"
    }
}

};
                    return( "hartie castiga");
                        }
            }
            else
            { 
                if(choice1 === "hartie") {
                                
                    if(choice2 === "foarfeca" ){

                        return( "foarfeca castiga");}
                    else {
                            return( "hartie castiga");
                            } 
                }
                else
                {
                 if(choice1 === "foarfeca" ) {
                     
                     if(choice2 == "hartie")

                        return( "foarfeca castiga");}
                    else {
                            return( "piatra castiga");
                    }    
                    }
                                


            }
}

#4

Hello good sir. You can find below the code. But I guess that codecademy should not let me pass any further if i had a bug in my code or if something is missing. But that's just a guess.


#5

Btw, there are some words translated in my language. It doesn't work with the words in english either.


#6

Ok at first this part:

var userChoice = prompt("Alegi piatra, hartie sau foarfeca?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
computerChoice = "piatra";
} else if(computerChoice <= 0.67) {
computerChoice = "hartie";
} else {
computerChoice = "foarfeca";
} console.log("Computer: " + computerChoice);
    var compare= function(choice1, choice2){
        if(choice1===choice2){
            return"The result is a tie!";
            }
        if(choice1 === "piatra")
            {
                if( (choice2 === "foarfeca")){

                    return( "piatra castiga");}
                else {

is kind of strange as I don't really get what you're trying there and it is made redundant by the following correct code:

var userChoice = prompt("Do you choose rock, hartie or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "piatra";
} else if(computerChoice <= 0.67) {
	computerChoice = "hartie";
} else {
 	computerChoice = "foarfeca";
} console.log("Computer: " + computerChoice);

As far as I can see this part seems to be ok as well although some parts are rather uncommon but not wrong:

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
    return "The result is a tie!"
} else if ( choice1 === "piatra") {
    if (choice2 === "foarfeca") {
        return "piatra castiga"
    }
    return "hartie castiga"
 } else if (choice1 === "paper") {
    if (choice2 === "piatra"){
        return "hartie castiga"
        }
        else {
        return "foarfeca castiga"
    }
}  else if (choice1 === "foarfeca") {
    if (choice2 === "piatra"){
         return "piatra castiga"
        }
        else {
        return "foarfeca castiga"
    }
}

};

But what comes after this is again at best redundant. What you need to do to see a result is to call the function by:

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

#7

Thanks for taking your time to answer to it. I'm kinda a rookie in here.. Where should i add console.log(compare(userChoice,computerChoice)); ? At the end of the code? After every if/else? Or..


#8

Anywhere after the declaration of your function:

var name = function(){
...
};

so yes in your case that would probably be at the very end of your code. No problem this is a beginner forum so that is just fine.


#9

Thanks! This really helped me solving my problem. It's working now. But, another problem is displayed now... When the pc is choosing "rock" which equals to "piatra" in my language, the console states "undefined"... I have looked in the code, but i don't really see a problem with it...


#10

Maybe it is really the language the interpreter is not able to translate as it is not able to see the meaning of a word for the interpreter it is just a "string ( of letters)" so as you might have no case for "rock" but only for "piatra" it might get confused. But as long as the code is working fixing the language should be possible. Can you post the link to the not-english lesson?


#11

You mean, to repost the code in here, but fully translated to english?


#12

What version of the exercise are you currently taking? The english one or the Romanian (? google translators choice). Depending on the exercise language should might have to stick to the names mentioned in the description to not confuse the test function.


#13

Oh, i understand now. I am taking the english one. I will translate all the words in english but now i have to go to work. I work as a game tester, and damn now i understand why the games are having so much bugs. Thank you for the time spent to answer my questions. Have a good day sir!


#14

You're welcome. Yes games are a pretty complicated thing :smile:
Then have a good day!


#15

why is using >>>console.log(compare(userChoice,computerChoice));<<< better? I just want some clarification for the future. thanx!


#16

I have another weird problem now... I changed the words in english, and there is a weird bug.. If i chose rock, and the computer choses paper, the computers choice is displayed, but another prompt is triggerd to the user to select one of them (rock, paper, scissors) and after the user selects one of them again, somehow the computer chooses the one that makes him win. It's kinda weird. Here is the code. I hope i explained it well, if not, try it in your own console to see if it happends the same thing...

var userChoice = prompt("Do you choose rock, hartie 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!";
    }
    if (choice1 === "rock") {
        if ((choice2 === "scissors")) {

            return ("rock wins");
        } else {
            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"
                    }
                    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"
                    }
                }

            };
            return ("paper wins");
        }
    } else {
        if (choice1 === "paper") {

            if (choice2 === "scissors") {

                return ("scissors wins");
            } else {
                return ("paper wins");
            }
        } else {
            if (choice1 === "scissors") {

                if (choice2 == "paper")

                    return ("scissors wins");
            } else {
                return ("rock wins");
            }
        }


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

#17

Seems like you still kept the redundant parts. Have a look at my 2nd post, the parts which are mentioned there as ok are the ones you really need the rest is not and I don't know why you have them in the first place :smile:

 var userChoice = prompt("Do you choose rock, hartie or scissors?");
var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "piatra";
} else if(computerChoice <= 0.67) {
	computerChoice = "hartie";
} else {
 	computerChoice = "foarfeca";
} console.log("Computer: " + computerChoice);

var compare = function(choice1, choice2) {
if (choice1 === choice2) {
return "The result is a tie!"
} else if ( choice1 === "piatra") {
if (choice2 === "foarfeca") {
    return "piatra castiga"
}
return "hartie castiga"
 } else if (choice1 === "paper") {
if (choice2 === "piatra"){
    return "hartie castiga"
    }
    else {
    return "foarfeca castiga"
}
}  else if (choice1 === "foarfeca") {
if (choice2 === "piatra"){
     return "piatra castiga"
    }
    else {
    return "foarfeca castiga"
}
}

};

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

this translated to english should do the job.

@wmwebb1125 if you just use compare what you see in the console is just the echo of the last value. So this is not really a reliable output but rather a feature of the console. console.log on the other hand is a reliable output so getting used to use a function to print stuff is probably better than just using a value and get it displayed in the console if and only if it is the last value in the code.


#18

You we're right... This helped me again..