23 work colleagues

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width+initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>地铁费计算</title>
  <style>
    div{
      margin-top:10px;
    }
    div>span{
      display: inline-block;
      width: 100px;
    }
  </style>
</head>

<body>
  <div>
    <span>单价:</span>
    <input type="text" id="price">
  </div>
  <div>
    <span>次数(每天):</span>
    <input type="text" id="num">
  </div>
  <div>
    <button onclick="jisuan()">计算</button>
  </div>
  <div id="wrap">
  </div>
</body>

</html>
<script>
  var dom = document.getElementById('wrap');
  var price = document.getElementById('price');
  var sum = document.getElementById('num');

  var subway = {
    init: function (price) {
      this.price = price;
      this.total = 0;
      this.year = 2018
      this.day_sum = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
      this.start = new Date(('2018-01-01')).getDay();
      this.week = null;
      this.get_week();
    },

    cal: function () {
      if (this.week === 6 || this.week === 0) return;
      this.total = this.total + this.price * this.coupon()
    },

    get_week: function () {
      if (this.week) {
        this.week = this.week + 1;
      } else {
        this.week = this.start
      }
      if (this.week > 6) {
        this.week = 0
      }
    },

    coupon: function () {
      if (this.total >= 100) return 0.8;
      if (this.total >= 150) return 0.5;
      return 1;
    },
  }

  function jisuan() {
    if (!price.value || !sum.value) {
      alert('请完善表单!');
      return;
    }
    dom.innerHTML = '';

    var total_money = 0;

    subway.init(price.value);
    for (var i = 0; i < subway.day_sum.length; i++) {
      for (var j = 0; j < subway.day_sum[i]; j++) {
        for (var a = 0; a < sum.value; a++) {
          subway.cal();
        }
        subway.get_week();
      }
      var div = document.createElement("div");
      var month_money = parseInt(subway.total * 10) / 10;
      div.innerHTML = i + 1 + '月地铁费:' + month_money;
      total_money += month_money;
      dom.appendChild(div);
      subway.total = 0;
    }
    var total_money = parseInt(total_money * 10) / 10
    var div = document.createElement("div");
    div.innerHTML = 'Total cost:' + total_money; 
    dom.appendChild (div); 
  } 
</ Script>

  

Guess you like

Origin www.cnblogs.com/gushixianqiancheng/p/10966035.html