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

标签 javascript html

在大学学习 JavaScript 的第一门类(class)。必须改变扑克游戏以添加总分等。最后添加了 updatetotalWinnings 功能,但无法正常工作。多年来一直在尝试不同的事情! html header 中调用的 Javascript 文件。开始时总奖金设置为 100。 html“total”行中的输出未定义。 'coin' 和 'winings' 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>";
}

非常感谢 Mike 非常清晰的解释,也感谢 Caleb、Mottie 和 mhodges 的回应。我已按照 Mike 和 Caleb 的建议进行操作,并宣布总奖金 = 100;位于函数上方(见下文)。我还尝试将该行放在定义函数 poker() 的 javascript 程序的开头(见下文),但都没有效果。我还按照 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 页面中的 id=“total”中显示 100 的总奖金,在 html 页面中的 id=“total2”中的 updatetotalWinnings 中显示未定义。

最佳答案

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

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 中,数字是“按值”传递的。这意味着变量值的副本被传递到函数中。由于它是副本,因此它不会更新原始变量。对象/数组“通过引用”传递。这与“按值”有何不同超出了本答案的范围,但将来很高兴知道。

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

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

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

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

关于javascript - 如何在javascript中保持运行总计?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36699349/

相关文章:

html - 从 Freemarker 中的相对链接获取绝对链接

c# - 在 C# 中生成 HTML 电子邮件正文

javascript - 我怎样才能始终在我的 jquery 移动 slider 上显示弹出窗口

javascript - 如何使用 html 表格单元格作为彩色像素显示字母?

javascript - 如何打开和关闭 Bulma 模型 Jquery 切换属性

Javascript函数获取两个数字之间的差异

javascript - jQuery .height() 无法正常运行

html - 像 CSS 布局一样的多列

javascript - 如何针对每个元素的所有内部类及其父级的 unique_id

javascript - window.onload 在 Firefox+Greasemonkey 脚本中有效,但在 Chrome 用户脚本中无效?