简体   繁体   English

如何在同一个函数中存储3个不同的结果

[英]How to store 3 different results within the same function

I`m 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 screen.Every play is stored in a different function that returns a value. 我正在使用javascript进行摇滚/纸/剪刀游戏,玩家将播放3次,每次的结果将显示在屏幕上。每次播放都存储在一个返回值的不同函数中。 The final result will be given according to the sum of those 3 values. 最终结果将根据这3个值的总和给出。

I am having problems to figure out the logic to play those 3 times in the same button. 我有问题找出在同一个按钮中播放3次的逻辑。

After I pressed a button and play by the first time, how can I press it again, but play a second function? 按下按钮并在第一次播放后,如何再次按下它,但播放第二个功能? Should I use only 1 function but how could I store 3 different results? 我应该只使用1个功能但是如何存储3个不同的结果?

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

Use an array - Each time use something like: 使用数组 - 每次使用类似的东西:

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

Then you will have a nice array of all the results. 然后你会得到一个很好的所有结果。 You can inspect the contents of the array with console.log(allResults) . 您可以使用console.log(allResults)检查数组的内容。 To access the individual results, use an index number, for example allResults[0] will return the first value in the array. 要访问单个结果,请使用索引号,例如allResults[0]将返回数组中的第一个值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将具有不同参数的相同JS函数存储在数组中并调用它们? - How to store same JS function with different parameters in array and call them? 具有不同随机结果的相同函数-jQuery第2部分 - Same function with different random results - jQuery part 2 JavaScript 在不同的时间内执行相同的函数 - JavaScript executes same function within different times 如何将JavaScript函数应用于同一类下的多个不同元素(结果不同)? - How can I apply JavaScript function to several different elements under the same class (with different results)? JS中的多个字符替换-如何在同一函数中停止替换以前的结果 - Multiple character replacements in JS - how to stop replacements on previous results within the same function 如何将异步(AJAX)函数的结果存储在变量中? - How to store the results of an async (AJAX) function in a variable? 如何在不同的设备上获得相同的结果? - How to get same results in different devices? 如何在每种情况下都解决不同的承诺,并将其结果传递给相同的功能? - How to resolve a different promise in each case of a switch block and pass their results to the same function? 在javascript中,我如何在具有相同类的多个div上运行一个函数以获得不同的结果 - In javascript how I run a function on multiple divs with the same class to get different results 将 function 结果存储到文本文件中 - store function results into textfile
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM