How to save 3 different results in the same function

I am working on a rock / paper / scissors game using javascript, the player will play 3 times, and the results of each time will be displayed on the screen. Each game is stored in a different function that returns a value. The final result will be given in accordance with the sum of these three values.

I am having trouble finding the logic to play these 3 times in the same button.

After I pressed the button and play the first time, how can I press it again, but play the second function? Should I use only 1 function, but how can I save 3 different results?

function playGame(choice) {


confirm('Are you sure?');
 document.getElementById("userChoice1").style.backgroundImage = "url('./img/rock2.png')";  
 document.getElementById("userChoice2").style.backgroundImage = "url('./img/scissors2.png')";  
 document.getElementById("userChoice3").style.backgroundImage = "url('./img/paper2.png')";  

   if(choice == 'A')
   {
      var userChoice = "rock";  
    /* animation   */	  
      document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/rock.png')";  
   }
   else if(choice ==  'B')
   {
      var userChoice = "scissors";   
	  /* animation   */	
      document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/scissors.png')";  
   } else if (choice ==  'C'){
      var userChoice = "paper";  
  	  /* animation   */	
      document.getElementById("userChoiceDis").style.backgroundImage = "url('./img/paper.png')";  
   }

var computerChoice = Math.random();
if (computerChoice < 0.34) {
	computerChoice = "rock";
	/* animation   */	
	document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/rock.png')";  
} else if(computerChoice <= 0.67) {
	computerChoice = "paper";
	/* animation   */	
	document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/paper.png')";  
} else {
	computerChoice = "scissors";
	/* animation   */	
	document.getElementById("aiChoiceDis").style.backgroundImage = "url('./img/scissors.png')"; 
} 

var compare1 = function(choice1, choice2) {

    if(choice1 === choice2) {
	    document.getElementById("result").style.backgroundImage = "url('./img/tie.png')"; 
		var pResult = "tie";
		document.getElementById("replay").style.display = "block";  
      
    }else if(choice1 ==="rock")
    {
        if(choice2 === "scissors")
        {
		    /* animation   */	
			
            document.getElementById("result").style.backgroundImage = "url('./img/win.png')"; 
			 
			/* animation   */	
			document.getElementById("replay").style.display = "block";  
			var pResult =  true;
			
        }else
        {
		    /* animation   */	
		    document.getElementById("result").style.backgroundImage = "url('./img/loose.png')"; 
			
			/* animation   */	
            document.getElementById("replay").style.display = "block";  
			var pResult =  false;
        }
    }else if(choice1 ==="paper")
    {
        if(choice2 === "rock")
        {
		    /* animation   */	
			document.getElementById("result").style.backgroundImage = "url('./img/win.png')"; 
			var pResult = true;
			/* animation   */	
			document.getElementById("replay").style.display = "block";  
        }else
        {
		     /* animation   */	
		    document.getElementById("result").style.backgroundImage = "url('./img/loose.png')"; 
			var pResult = false;
			/* animation   */	
			document.getElementById("replay").style.display = "block";  
        }
    }else if(choice1 ==="scissors")
    {
        if(choice2 === "rock")
        {
		    /* animation   */	
            document.getElementById("result").style.backgroundImage = "url('./img/loose.png')"; 
			var pResult = false;
			/* animation   */	
		    document.getElementById("replay").style.display = "block";  
			
        }else
        {
		    /* animation   */	
		    document.getElementById("result").style.backgroundImage = "url('./img/win.png')"; 
			var pResult = true;
			/* animation   */	
			document.getElementById("replay").style.display = "block";  
        }
    }else
    {
         return "incorrect input";
    }
	
	switch(pResult) {
    case true:
        document.getElementById("gameResult1").style.backgroundImage = "url('./img/win.png')";
        break;
    case "tie":
        document.getElementById("gameResult1").style.backgroundImage = "url('./img/win.png')";
        break;
    default:
        document.getElementById("gameResult1").style.backgroundImage = "url('./img/loose.png')";
		
}
	
};
compare1(userChoice,computerChoice);
 
 
Run codeHide result
+4
source share
1 answer

Using an array - use something like each time:

var allResults = []; //declare the array
allResults.push(results);

. console.log(allResults). , , allResults[0] .

+1

All Articles