How to trigger my function? it has some errors


#1


ar attacks = [ "rock",
"scissors",
"paper"];

var compAttack = Math.floor(Math.random() * attacks.length);
var humanAttack = prompt("What attack would you like to use? rock, scissors, paper?")

var game = function(compAttack, humanAttack){
 if(compAttack === humanAttack)
 {
     return "Draw!";
 }else if(compAttack = "rock" && humanAttack = "scissors")
    {
        return "Comp WIN!";
    }else
    if(compAttack === "paper" && humanAttack === "rock")
    {
        return "Comp WIN!";
    }else if(compAttack === "scissors" && humanAttack === "paper")
    {
        return "Comp WIN!";
    }else
    {
    return "You WIM!";
    }
 
}
game(compAttack, humanAttack);

// I hope to trigger the function by getting input variables by auto generating random, and prompting user. how can I do it?  pls advice ty.


#2

triggering your function doesn't help much if there is an error

here:

humanAttack = "scissors")

that can't be right, a single equal sign means assgn, and that in else if condition


#3

Thanks for replying, I have found my best set of answer.

var attacks = [ "rock",
"scissors",
"paper"];
var computerAttackraw= Math.floor(Math.random() * attacks.length);
var compAttack = attacks[computerAttackraw];
var humanAttack = prompt("What attack would you like to use? rock, scissors, paper?");
console.log("You chose "+humanAttack);
console.log("Computer chose "+compAttack);

var game = function(compAttack, humanAttack){
if(compAttack === humanAttack)
{
return "Draw!";
}else if(compAttack === "rock" && humanAttack === "scissors")
{
return "Comp WIN!";
}else
if(compAttack === "paper" && humanAttack === "rock")
{
return "Comp WIN!";
}else if(compAttack === "scissors" && humanAttack === "paper")
{
return "Comp WIN!";
}else if(humanAttack !== "paper" || humanAttack !=="paper" ||humanAttack!=="scissors")
{
return "You have chosen an invalid input";
}else
{
return "You WIM!";
}

}
//var compAttack = Math.floor(Math.random() * attacks.length);
//var humanAttack = prompt("What attack would you like to use? rock, scissors, paper?")

game(compAttack, humanAttack);


#4

the human/player can't win at the moment? That is kind of unfair :wink:

the problem is here:

else if(humanAttack !== "paper" || humanAttack !=="paper" ||humanAttack!=="scissors")

when the human picks something valid (lets say paper), we get:

else if(false || true || true)

which is true, so you get: not a valid choice


#5

Nice, you are sharp. hahah this is why programming is annoying yet fun at the same time. Thanks for your feedback.
took me awhile to finally solve it. Let me know if you spot more mistakes.

var attacks = [ "rock",
"scissors",
"paper"];
var computerAttackraw= Math.floor(Math.random() * attacks.length);
var compAttack = attacks[computerAttackraw];
var humanAttack = prompt("What attack would you like to use? rock, scissors, paper?");
console.log("You chose "+humanAttack);
console.log("Computer chose "+compAttack);

var game = function(compAttack, humanAttack){
if(compAttack === humanAttack)
{
return "Draw!";
}else if(compAttack === "rock" && humanAttack === "scissors")
{
return "Comp WIN!";
}else
if(compAttack === "paper" && humanAttack === "rock")
{
return "Comp WIN!";
}else if(compAttack === "scissors" && humanAttack === "paper")
{
return "Comp WIN!";
}else if(humanAttack !== "rock" && humanAttack !=="paper" && humanAttack !=="scissors")
{
return "You have chosen an invalid input";
}else
{
return "You WIM!";
}

}

game(compAttack, humanAttack);


#6

absolutely true. Seems all good now :slight_smile:


#7

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