0

I have created a basic rock paper scissors game and i cannot return the result inside the function.

I've been try everything

link console logging the compare and also putting it inside a variable..

console.log(compare) returns undefined for some reason.

Please help

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
 computerChoice = "rock";
} else if(computerChoice <= 0.67) {
 computerChoice = "paper";
} else {
 computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 
  

var compare = function(userChoice, computerChoice){
    if (userChoice === computerChoice){
        return    "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }
     
}
Redondo Velasco
  • 103
  • 1
  • 8
  • 3
    `return = "The result is a tie!";`? Why the equal sign? – Shinra tensei Jul 24 '17 at 10:32
  • oh that was an honest typo but having it removed, I stil have the same problem – Redondo Velasco Jul 24 '17 at 10:35
  • 1
    Well I tested the snippet and it works fine. I think you believe it doesn't return anything because you don't do anything with the returned string – Shinra tensei Jul 24 '17 at 10:37
  • @Shinratensei so maybe my problem is how can i actually display that returned string? – Redondo Velasco Jul 24 '17 at 10:39
  • well, `compare` stores the string returned by the function, so just `console.log(compare);` at the end maybe? Try but I'm not sure, I don't know javascript – Shinra tensei Jul 24 '17 at 10:41
  • @Shinratensei I've tried that already but it returns undefined! – Redondo Velasco Jul 24 '17 at 10:42
  • 1
    you can make compare a function and then `alert(compare(userChoice,computerChoice))` – Colin Jul 24 '17 at 10:43
  • @Colin is compare not a function already? – Redondo Velasco Jul 24 '17 at 10:48
  • never mind.. you have to make it "function compare (userChoice, computerChoice);" thanks for the help! – Redondo Velasco Jul 24 '17 at 10:51
  • It is a variable that is a function. I don't know the diference to be honest. Look at my snippet in my answer. I changed it and it works. – Colin Jul 24 '17 at 10:51
  • That function is wrong for that what you want. U can write like function compare(comp, user){ ... } and call console.log(compare(comp,user)) or if u want to store to variable, u can first write var Compare = compare(comp,user); than console.log(Compare); – Deathmras Jul 24 '17 at 10:51
  • 1
    Here is the difference explained: https://stackoverflow.com/questions/4314434/difference-between-var-foo-function-and-function-foo – Colin Jul 24 '17 at 10:52
  • @RedondoVelasco you see, that was an anonymous function. Basically, functions are objects in javascript and as such, variables can store them. They're different from other functions, I recommend reading this http://helephant.com/2008/08/23/javascript-anonymous-functions/ – Shinra tensei Jul 24 '17 at 10:55

4 Answers4

2

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
 computerChoice = "rock";
} else if(computerChoice <= 0.67) {
 computerChoice = "paper";
} else {
 computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 

function compare(userChoice, computerChoice){
    if (userChoice === computerChoice){
        return    "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }
     
}
var Compare = compare(userChoice, computerChoice);
console.log(Compare);
Deathmras
  • 312
  • 1
  • 9
  • But if you want actually same code like u did there, u can use something like var compare = (function(comp,user){ ... })() – Deathmras Jul 24 '17 at 10:57
0

You have syntax error here...

return  =  "The result is a tie!"; 

change this to:

return "The result is a tie!"; 
Pommesloch
  • 492
  • 5
  • 17
  • That was an honest typo. But actually I'm still having the same problem! maybe because i cant console log the right value? – Redondo Velasco Jul 24 '17 at 10:38
  • @RedondoVelasco you need to call `compare();`at the bottom of your code for display the return statement. Otherwise you can just `console.log()`the string. – Pommesloch Jul 24 '17 at 10:46
0

As Shinra tensei mentioned you had a = in line 18 that caused an error. Here is a updated snipped. I also added an alert for the compare function.

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
 computerChoice = "rock";
} else if(computerChoice <= 0.67) {
 computerChoice = "paper";
} else {
 computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 
  
  alert(compare(userChoice,computerChoice));

 function compare (userChoice, computerChoice){
    if (userChoice === computerChoice){
        return  "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }
     
}
Colin
  • 1,112
  • 1
  • 16
  • 27
0

The main problem here is that you never executed the anonymous function you wrote. I made compare a normal function instead of an anonymous one, just to make it more similar to other languages. This is a good working code:

var userChoice = prompt("Do you choose rock, paper or scissors?");
var computerChoice = Math.random();
var result;

if (computerChoice < 0.34) {
    computerChoice = "rock";
} else if(computerChoice <= 0.67) {
    computerChoice = "paper";
} else {
    computerChoice = "scissors";
} console.log("Computer's choice: " + computerChoice);
  console.log("Your choice: " + userChoice);
  console.log(computerChoice + " vs " + userChoice); 


function compare(userChoice, computerChoice){
    if (userChoice === computerChoice){
        return    "The result is a tie!"; 
    } else if (userChoice ==="rock"){
       if(computerChoice ==="scissors"){
        return "rock wins!";    
       }
       else {
         return "paper wins!";       
       }
    } else if (userChoice === "paper") {
        if(computerChoice === "rock") {
        return "paper wins!"; 
        }
        else {
        return "scissors wins!";
        }
    } else if (userChoice === "scissors") {
        if(computerChoice === "rock") {
        return "rock wins!"; 
        }
        else {
        return "scissors wins!";
        }
    }

}
console.log(compare(userChoice, computerChoice));
Shinra tensei
  • 1,283
  • 9
  • 21