Jump to content

JavaScript function not working

Cookiecrumbles222
Go to solution Solved by keskparane,

When you call a function none of the if statements are being called.

/* If user click rock/paper/cuter */

var playerChoice = 0;
var computerChoice = 0;
var result = 0;
var playerTotal = 0;
var computerTotal = 0;



function rockf(){
     playerChoice = 1;
     computerChoice = Math.floor((Math.random() * 3) + 1);
}
function paperf(){
     playerChoice = 2;
     computerChoice = Math.floor((Math.random() * 3) + 1);
}
function cuterf(){
     playerChoice = 3;
     computerChoice = Math.floor((Math.random() * 3) + 1);
}


function winFunct(){
    document.getElementById("info").innerHTML = "<h1>You Win</h1>";
    playerChoice = 0;
    computerChoice = 0;
    result = 0;
    playerTotal = 0;
    computerTotal = 0;
}
function loseFunct(){
    document.getElementById("info").innerHTML = "<h1>You Lose</h1>";
    playerChoice = 0;
    computerChoice = 0;
    result = 0;
    playerTotal = 0;
    computerTotal = 0;
}
function tieFunct(){
    document.getElementById("info").innerHTML = "<h1>You Tied</h1>";
    playerChoice = 0;
    computerChoice = 0;
    result = 0;
    playerTotal = 0;
    computerTotal = 0;
}


function computerWin(){
    
}
function playerWin(){
    
}


function startGame(){
    playerChoice = 0;
    computerChoice = 0;
    result = 0;
    playerTotal = 0;
    computerTotal = 0;
}

/* 1 = win || 2 = loss || 3 = tie */
if(playerChoice == 1 && computerChoice == 1){
   result = 3;
 }
if(playerChoice == 1 && computerChoice == 2){
   result = 2;
 }
if(playerChoice == 1 && computerChoice == 3){
   result = 1;
 }

if(playerChoice == 2 && computerChoice == 1){
   result = 1;
 }
if(playerChoice == 2 && computerChoice == 2){
   result = 3;
 }
if(playerChoice == 2 && computerChoice == 3){
   result = 2;
 }

if(playerChoice == 3 && computerChoice == 1){
   result = 2;
 }
if(playerChoice == 3 && computerChoice == 2){
   result = 1;
 }
if(playerChoice == 3 && computerChoice == 3){
   result = 3;
 }

if(result == 1){
    winFunct();
}
if(result == 2){
    loseFunct();
}
if(result == 3){
    tieFunct();
}

Hey guys, above is a javascript function I'm working on trying to learn javascript and I've encountered a problem. its a program that is meant to play rock paper scissors, but it won't calculate who won, I have buttons that activate the rockf, paperf, and cuterf functions and I know those are being activated properly but I can't figure out why they are not working. it should return either "you win", "you lose", or "you tied" but nothing changes. Thanks for any help!

 

Edit: also the document.getElementById("info").innerHTML = "<h1>You Tied</h1>"; code works properly i tested it by putting it in the rockf function and activating it and it worked fine

Link to comment
Share on other sites

Link to post
Share on other sites

When you call a function none of the if statements are being called.

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, keskparane said:

When you call a function none of the if statements are being called.

do i have to put all the if statements in each function?

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Cookiecrumbles222 said:

do i have to put all the if statements in each function?

 

5 minutes ago, keskparane said:

When you call a function none of the if statements are being called.

just kidding i figured it out thanks for the help!

Link to comment
Share on other sites

Link to post
Share on other sites

Seeing as your adding to keep score why not compare the numbers to check who won?

 

If player === computer draw

If player > computer player win

Else computer win

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×