Advertisement
zarkologiest

pembulatan

Aug 3rd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 1.61 KB | None | 0 0
  1. <html>
  2.  <body>
  3.  
  4.   <div id="button_list">
  5.   </div>
  6.   <br />
  7.   <div id="total_bet">
  8.   </div>
  9.   <br />
  10.   <div id="chip_list">
  11.   </div>
  12.  
  13.   <script type="text/javascript">
  14.  
  15.    var chip_types = [1000, 5000, 10000, 25000, 50000, 100000];
  16.    var total_bet = 0;
  17.  
  18.    build();
  19.  
  20.    function build() {
  21.  
  22.     var button_list = document.getElementById("button_list");
  23.  
  24.     chip_types.forEach(function(chip_type) {
  25.      var node = document.createElement("button");
  26.      var textnode = document.createTextNode(chip_type);
  27.  
  28.      node.appendChild(textnode);
  29.  
  30.      node.onclick = function() {
  31.       addBet(chip_type);
  32.      };
  33.  
  34.      button_list.appendChild(node);
  35.     });
  36.  
  37.    }
  38.  
  39.    function addBet(bet) {
  40.     total_bet += bet;
  41.  
  42.     var text_total_bet = document.getElementById("total_bet");
  43.     text_total_bet.innerHTML = "Total bet  : " + total_bet;
  44.  
  45.     showChips();
  46.    };
  47.  
  48.    function showChips() {
  49.  
  50.     var chip_list = document.getElementById("chip_list");
  51.     var chips = calculateChips();
  52.     var text_chip_list = "Chips : <br/>";
  53.  
  54.     for (var i = 0; i < chips.length; i++) {
  55.     for (var j = 0; j < chips[i]; j++) {
  56.      text_chip_list += chip_types[i] + "<br/>";
  57.      };
  58.     };
  59.  
  60.     chip_list.innerHTML = text_chip_list;
  61.    };
  62.  
  63.    function calculateChips() {
  64.     var result = [];
  65.     var temp_total_bet = total_bet;
  66.  
  67.     for (var i = chip_types.length - 1; i >= 0; i--) {
  68.      var total_chip = Math.floor(temp_total_bet / chip_types[i]);
  69.      result.unshift(total_chip);
  70.      temp_total_bet -= total_chip * chip_types[i];
  71.     };
  72.  
  73.     return result;
  74.    }
  75.  
  76.   </script>
  77.  </body>
  78. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement