如何在javascript中保持运行总计?

时间:2016-04-18 16:21:03

标签: javascript html

在大学里练习javascript的第一门课程。必须改变扑克游戏以增加跑步总数等。最后添加了功能updatetotalWinnings但无法正常工作。多年来一直在尝试不同的事情!在html的标题中调用的Javascript文件。 totalWinnings在开始时设置为100。 html'total'行的输出未定义。 'coin'和'winnings'html行给出了正确的输出。请帮忙!

function determineWinnings() {
    var moneyStr;
    var winnings;

    var money = [0, 10, 20, 30, 60, 40, 70, 50, 80, 90];
    moneyStr += "<BR>" + "Winnings for this hand are: " + money[winType] + "    cents";
    winnings = money[winType];

    document.getElementById("money").innerHTML = "<p> " + moneyStr + "</p>"; // This prints "Winnings for this hand are: money[winType] "

    document.getElementById("coin").innerHTML = "<p> " + winnings + "</p>";
    document.getElementById("winnings").src = "coins/" + money[winType] + ".png";

}

function updatetotalWinnings(winnings, totalWinnings) {
    if (winnings > 0) {
        totalWinnings += winnings;
    }

    document.getElementById("total").innerHTML = "<p> " + totalWinnings + "</p>";
}

非常感谢迈克非常明确的解释,感谢Caleb,Mottie和mhodges也做出了回应。我已经完成了迈克和迦勒的建议并宣布totalWinnings = 100;功能上方(见下文)。我也尝试将该行放在javascript程序的开头,其中定义了函数poker()(见下文),两者都没有效果。我还按照Mottie的要求提供了html。

var handSize, suits, values, cards, result, winType;    // Defines global variables handsize, suits, values, cards, result, wintype.
var moneyStr, money, winnings;
var totalWinnings = 100;
function poker()
{   
  handSize = 5;                     
  suits = new Array(handSize);      
  values = new Array(handSize);     
  cards = new Array(handSize);      
  result = "";                  
  generateUniqueHandOfCards();
  determineSuitsAndValues();
  orderValuesInDescendingSequence();
  determineCardDescriptions();
  evaluateHandOfCards();
  determineWinType();
  determineWinnings();
  updatetotalWinnings();


  //document.write(result);   
  document.getElementById("cardPara").innerHTML = result;
  document.getElementById("total").innerHTML = "<p>" + totalWinnings + "</p>";
  document.getElementById("total2").innerHTML = updatetotalWinnings();
 }


function determineWinnings()
{
var moneyStr;


  var money = [0, 10, 20, 30, 60, 40, 70, 50, 80, 90];
  moneyStr += "<BR>" + "Winnings for this hand are: " + money[winType] + " cents";  // Note money[winType] is a number giving the cents won.
  winnings =  money[winType];

// document.getElementById("money").innerHTML = "<p> " + moneyStr + "</p>"; // This prints "Winnings for this hand are: money[winType] "

   document.getElementById("coin").innerHTML = "<p> " + winnings + "</p>"; // Prints winnings without accompanying text.
   document.getElementById("winning").src = "coins/" + money[winType] + ".png"; // Note since money[winType] is not a string it is not in quotes.

}
var totalWinnings = 100;

function updatetotalWinnings(winnings)
{
    if (winnings > 0){

        totalWinnings += winnings;

    }


}

<!DOCTYPE html>
<html>
  <head><title>Assignment 2 Poker Game</title>
    <script src= "PokerAssignment10.js">
    </script>
  </head>
  <body>



<button id="button1" onclick="poker()"> Run Poker Game! </button>

    <p> </p> <!-- Inserts a blank paragraph after the Run Poker Game! button -->    


<!-- Lines 21-25 initially put five card_back images side by side.
    Once the function poker has run by clicking on  "Run Poker Game" button, 
    the id="card"+i flag is used to put the image for each card dealt side-by-side.
-->

<img id="card0" src="card_back.png" alt="card_back" width="100" height="145">  
<img id="card1" src="card_back.png" alt="card_back" width="100" height="145">
<img id="card2" src="card_back.png" alt="card_back" width="100" height="145">
<img id="card3" src="card_back.png" alt="card_back" width="100" height="145">
<img id="card4" src="card_back.png" alt="card_back" width="100" height="145">

    <p> </p>
    <p> </p>

    <p id="cardPara"> Hand type dealt will appear here </p>




<p id="money"> </p>



<p id="coin"> </p> 

<p>
<img id="winning"  alt="Winnings will appear here" height="60" > 
<!-- Note leaving out the img src= attribute, html lists the alt= text string "Winnings will appear here" instead  -->
<!-- Using document.getElementById("winning").src =  source location in javascript then replaces text string with image -->
</p>

<p id="total"> </p>

<p id="total2"> </p>


  </body>
</html>

我仍然在html页面中显示100为id =“total”的totalWinnings,并且在html页面中为id =“total2”中的updatetotalWinnings显示未定义。

1 个答案:

答案 0 :(得分:0)

这样的情况向您展示了不同范围的有用性。考虑这个例子:

function add2() {
  var total = 0;
  total += 2;
  console.log(total);
}

在这种情况下,总是打印2,因为total是在函数内部声明的。如果我们将变量传递给函数,就会出现同样的问题。

function add2(total) {
  total += 2;
  console.log(total);
}
var total = 0;
add2(total); // 2
add2(total); // 2
console.log(total); // 0

在这种情况下,您传入了一个外部变量,但它没有更新。那是什么?在Javascript中,数字通过值&#34;传递给#34;。这意味着将变量值的副本传递给函数。由于它是副本,因此不会更新原始变量。对象/数组通过引用&#34;传递。它与&#34;与价值的关系如何&#34;超出了这个答案的范围,但将来很有用。

那么我们如何获得持久价值呢?一种方法是声明函数的外部,然后在函数内部引用

var total = 0;
function add2() {
  total += 2;
  console.log(total);
}
add2(); // 2
add2(); // 4
add2(); // 6

这是有效的,因为我们每次调用函数时都会引用实际的原始变量,而不是创建副本或创建新变量。

因此,解决问题的最简单方法是在函数外部声明totalWinnings ,然后在其他函数的内引用它