Completed build of game, but limited functionality


#1


Hello, all! This is my first JavaScript course (just a few hours in)
and, coincidentally, my first post. I successfully completed
this exercise (the system isn't throwing any errors) but I'm still
left wondering if there's a mistake somewhere: specifically,
the "prompt" box works, the user makes one move, the computer
makes its first move (which is shown in the console) but then...
that's it—there's no "The result is a tie!" or "Rock wins!"
I thought this would be accomplished with "return," but perhaps
I need "console.log" to display this. Any feedback whatsoever would be
greatly appreciated. Thanks!



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


#2

Lets dissect your code and see if there're any bug available!

First part--

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);

This parts takes input from user.
This part also generates random guess from computer.

Lets see the second part of code...

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";
        } // SOMETHING YOU MISSED HERE?????
    } 
   
};

Lets see what this code does,

  1. You defined a function: It's main purpose is to compare the guess of user to computers, Then return who won!

  2. Inside the function there are if/else if / else statements

a. It checks if choice of computer and user are equal, If it is then it returns '"The result is a tie!"' .
(choice1 === choice2)

b. In else if block you checked about this
(choice1 === "rock")

c.In second else if block you checked about this
(choice1 === "paper")

d. I'm the final Missing thing....

But you did not checked one more condition????

Your code is running fine ! yeah!
But you don't see any error?
This type of error or bug is called Logical Error.

More about Logical Error here--
Read about it: I'm logical Error. I don't crash your code but will create wrong outputs

I hope you get the hint! :slight_smile:
do reply if it solved your problem!!:slight_smile:


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.