Javascript 我需要从我的游戏中返回一个变量

Javascript 我需要从我的游戏中返回一个变量,javascript,variables,loops,methods,Javascript,Variables,Loops,Methods,因为我现在有了这个游戏,它会根据用户的选择循环很多次,我需要取玩的数量并除以赢的数量来获得赢的百分比。我不知道该怎么做,所以非常感谢您的帮助。提前谢谢 function rockPaperScissors(){ function game(wantToPlay){ if (wantToPlay=="Yes"||wantToPlay=="yes"){ var userChoice = prompt("

因为我现在有了这个游戏,它会根据用户的选择循环很多次,我需要取玩的数量并除以赢的数量来获得赢的百分比。我不知道该怎么做,所以非常感谢您的帮助。提前谢谢

 function rockPaperScissors(){
            function game(wantToPlay){
                if (wantToPlay=="Yes"||wantToPlay=="yes"){
                    var userChoice = prompt("Do you choose rock, paper or scissors?");
                    //gets a random number
                    var computerChoice = Math.random();

                    //assigns that random number to be either rock, paper or scissors based on the number
                    if (computerChoice < .34) {
                        computerChoice = "rock";}
                    else if(computerChoice <= .66) {
                        computerChoice = "paper";}
                    else {
                        computerChoice = "scissors";
                    }
                    console.log("computerChoice: " + computerChoice);
                    console.log("userChoice: " + userChoice);

                    //compares the computer choice to the user choice and then prints out the outcome on the page.
                    function compare(choice1,choice2){
                        console.log(choice1 + "<-----User");
                        console.log(choice2 + "<-----Computer");
                        if (choice1==choice2){
                            alert("It was a tie!");
                            game("yes");
                        }
                        if (choice1=="rock"){
                            if (choice2=="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Rock crushes scissors.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";

                            }
                            /**else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="paper"){
                            if (choice2=="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Paper smothers rock.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="scissors"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";

                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Paper smothers rock.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else if (choice1=="scissors"){
                            if (choice2=="paper"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="You win. Scissors cut paper.";
                                document.getElementById("loss").innerHTML="";
                            }
                            if (choice2 =="rock"){
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("win").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Rock crushes scissors.";
                            }
                           /** else{
                                document.getElementById("messages").innerHTML="";
                                document.getElementById("loss").innerHTML="You lose. Scissors cut paper.";
                                document.getElementById("win").innerHTML="";
                            }**/
                        }
                        else{
                            alert("Very funny. Read the help menu and do it right.");
                            game("yes");
                        }

                    };
                    compare(userChoice,computerChoice);
                }

                //this only runs if the user does not type yes
                else{
                    document.getElementById("messages").innerHTML="Well alrighty then.";
                    document.getElementById("loss").innerHTML="";
                    document.getElementById("win").innerHTML="";
                }
            }
            //promts the start of the game and loops x amount of times based on user input.
            var start = prompt ("Do you want to play?","Yes");
            var i = prompt("How many times do you want to play?", 5);
            for (n = 0; n < i; n ++){
            game(start);
        }}
功能:剪纸机(){
功能游戏(WANTOPLAY){
如果(WANTOPLAY==“是”| WANTOPLAY==“是”){
var userChoice=prompt(“您选择石头、布还是剪刀?”);
//获取一个随机数
var computerChoice=Math.random();
//根据数字将该随机数指定为石头、布或剪刀
如果(计算机选择<.34){
computerChoice=“rock”}

else if(computerChoice声明变量,如下所示(在
中并创建函数
initVars()

当玩家赢了,你就赢了+++
玩+++
,当玩家输了-只有
玩+++


当游戏结束时,你做
wins/plays

Protip:将元素存储为变量,并停止滥用
innerHTML
D:你必须计算赢和输。似乎你可以很容易地做到这一点,只需在if语句中增加几个变量,然后将其除以玩过的游戏数即可得到per百分之…cale_b如果在if语句中工作,那么游戏是循环的,那么每次循环游戏时,赢和输的变量不会重置为其指定的值吗?我想最简单的方法是尝试一下。这不起作用,它不会保存增加的值,只是在每次游戏循环时重置它
var wins=0;
var plays=0;