18910140161

Javascript变量与数组的效率&将它们输入到HTML-Stack溢出

顺晟科技

2022-10-19 11:50:36

214

我正在制作一个页面,它向用户提问,获取结果,获取分配给该结果的变量,并将它们输入到一个表中。到目前为止,我所做的工作很好,但效率不高,我知道有更好的方法可以使用数组和循环来完成,但我尝试的每个迭代仍然会产生等于或更多的工作。 以下是我所创建的内容(它工作得很好):

我考虑过使用这样的数组:

    function agecalc() {
      var weight;
      var HRlow;
      var HRhi;
      var RRlow;
      var RRhi;
      var SBPlow;
      var SBPhi;
      var DBPlow;
      var DBPhi;
      var age = parseFloat(document.getElementById("agechoice").value);
      if (isNaN(age)) {
        var weight = 0
      } else if (age == 0) {
        var weight = 3.5
      } else if (age == 0.5) {
        var weight = 8
      } else if (age == 8) {
        var weight = 25
      } else if (age == 9) {
        var weight = 28
      } else if (age == 10) {
        var weight = 32
      } else if (age == 11) {
        var weight = 36
      } else {
        var weight = (age + 4) * 2
      }
    
      // Observations determinant
      if (weight >= 0 && weight <= 5) {
        var HRlow = 100,
          HRhi = 160,
          RRlow = 30,
          RRhi = 50,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 5 && weight <= 9) {
        var HRlow = 80,
          HRhi = 140,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 9 && weight <= 11) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 11 && weight <= 13) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 13 && weight <= 15) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 15 && weight <= 17) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 17 && weight <= 19) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 19 && weight <= 21) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 21 && weight <= 23.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 23.5 && weight <= 26.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 26.5 && weight <= 30) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 30 && weight <= 34) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 34 && weight <= 38) {
        var HRlow = 60,
          HRhi = 105,
          RRlow = 15,
          RRhi = 20,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 38 && weight <= 49) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 49 && weight <= 69) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 69 && weight <= 99) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight >= 100) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      }
      // Place observations into the table
      document.getElementById("HRlow").innerHTML = HRlow;
      document.getElementById("HRhi").innerHTML = HRhi;
      document.getElementById("RRlow").innerHTML = RRlow;
      document.getElementById("RRhi").innerHTML = RRhi;
      document.getElementById("SBPlow").innerHTML = SBPlow;
      document.getElementById("SBPhi").innerHTML = SBPhi;
      document.getElementById("DBPlow").innerHTML = DBPlow;
      document.getElementById("DBPhi").innerHTML = DBPhi;
    }

但是,在每个分组下(例如:(weight>=0&&weight<=5)),我是否必须放:

    function agecalc() {
      var weight;
      var HRlow;
      var HRhi;
      var RRlow;
      var RRhi;
      var SBPlow;
      var SBPhi;
      var DBPlow;
      var DBPhi;
      var age = parseFloat(document.getElementById("agechoice").value);
      if (isNaN(age)) {
        var weight = 0
      } else if (age == 0) {
        var weight = 3.5
      } else if (age == 0.5) {
        var weight = 8
      } else if (age == 8) {
        var weight = 25
      } else if (age == 9) {
        var weight = 28
      } else if (age == 10) {
        var weight = 32
      } else if (age == 11) {
        var weight = 36
      } else {
        var weight = (age + 4) * 2
      }
    
      // Observations determinant
      if (weight >= 0 && weight <= 5) {
        var HRlow = 100,
          HRhi = 160,
          RRlow = 30,
          RRhi = 50,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 5 && weight <= 9) {
        var HRlow = 80,
          HRhi = 140,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 9 && weight <= 11) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 11 && weight <= 13) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 13 && weight <= 15) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 15 && weight <= 17) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 17 && weight <= 19) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 19 && weight <= 21) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 21 && weight <= 23.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 23.5 && weight <= 26.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 26.5 && weight <= 30) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 30 && weight <= 34) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 34 && weight <= 38) {
        var HRlow = 60,
          HRhi = 105,
          RRlow = 15,
          RRhi = 20,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 38 && weight <= 49) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 49 && weight <= 69) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 69 && weight <= 99) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight >= 100) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      }
      // Place observations into the table
      document.getElementById("HRlow").innerHTML = HRlow;
      document.getElementById("HRhi").innerHTML = HRhi;
      document.getElementById("RRlow").innerHTML = RRlow;
      document.getElementById("RRhi").innerHTML = RRhi;
      document.getElementById("SBPlow").innerHTML = SBPlow;
      document.getElementById("SBPhi").innerHTML = SBPhi;
      document.getElementById("DBPlow").innerHTML = DBPlow;
      document.getElementById("DBPhi").innerHTML = DBPhi;
    }

等,从而产生更多代码。

我考虑过按年龄组设置数组,例如

    function agecalc() {
      var weight;
      var HRlow;
      var HRhi;
      var RRlow;
      var RRhi;
      var SBPlow;
      var SBPhi;
      var DBPlow;
      var DBPhi;
      var age = parseFloat(document.getElementById("agechoice").value);
      if (isNaN(age)) {
        var weight = 0
      } else if (age == 0) {
        var weight = 3.5
      } else if (age == 0.5) {
        var weight = 8
      } else if (age == 8) {
        var weight = 25
      } else if (age == 9) {
        var weight = 28
      } else if (age == 10) {
        var weight = 32
      } else if (age == 11) {
        var weight = 36
      } else {
        var weight = (age + 4) * 2
      }
    
      // Observations determinant
      if (weight >= 0 && weight <= 5) {
        var HRlow = 100,
          HRhi = 160,
          RRlow = 30,
          RRhi = 50,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 5 && weight <= 9) {
        var HRlow = 80,
          HRhi = 140,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 9 && weight <= 11) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 11 && weight <= 13) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 13 && weight <= 15) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 15 && weight <= 17) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 17 && weight <= 19) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 19 && weight <= 21) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 21 && weight <= 23.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 23.5 && weight <= 26.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 26.5 && weight <= 30) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 30 && weight <= 34) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 34 && weight <= 38) {
        var HRlow = 60,
          HRhi = 105,
          RRlow = 15,
          RRhi = 20,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 38 && weight <= 49) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 49 && weight <= 69) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 69 && weight <= 99) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight >= 100) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      }
      // Place observations into the table
      document.getElementById("HRlow").innerHTML = HRlow;
      document.getElementById("HRhi").innerHTML = HRhi;
      document.getElementById("RRlow").innerHTML = RRlow;
      document.getElementById("RRhi").innerHTML = RRhi;
      document.getElementById("SBPlow").innerHTML = SBPlow;
      document.getElementById("SBPhi").innerHTML = SBPhi;
      document.getElementById("DBPlow").innerHTML = DBPlow;
      document.getElementById("DBPhi").innerHTML = DBPhi;
    }

但是我必须再次在每个分组下输入document.getElementById的每个迭代并选择对象。 我知道有一种方法可以更有效地完成它(也许用循环?)我就是不能把我的手指放在上面。


顺晟科技:

您可以在数组数组中定义所有可能的等等。每个子数组可以对应一个条件-例如一个子数组是for的,另一个子数组是for的。

还有一个条件数组--前面提到的条件。找到第一个true条件的索引,然后使用该索引查找适当的子数组。

使您想要插入数据的TDs更容易选择--例如使用特定的类--然后您可以迭代标识的子数组并插入到每个子数组中。例如,如果您给要填充的TDs指定类:

还可以创建年龄到体重的映射,而不是开始时的/s。

    function agecalc() {
      var weight;
      var HRlow;
      var HRhi;
      var RRlow;
      var RRhi;
      var SBPlow;
      var SBPhi;
      var DBPlow;
      var DBPhi;
      var age = parseFloat(document.getElementById("agechoice").value);
      if (isNaN(age)) {
        var weight = 0
      } else if (age == 0) {
        var weight = 3.5
      } else if (age == 0.5) {
        var weight = 8
      } else if (age == 8) {
        var weight = 25
      } else if (age == 9) {
        var weight = 28
      } else if (age == 10) {
        var weight = 32
      } else if (age == 11) {
        var weight = 36
      } else {
        var weight = (age + 4) * 2
      }
    
      // Observations determinant
      if (weight >= 0 && weight <= 5) {
        var HRlow = 100,
          HRhi = 160,
          RRlow = 30,
          RRhi = 50,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 5 && weight <= 9) {
        var HRlow = 80,
          HRhi = 140,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 75,
          SBPhi = 100,
          DBPlow = 50,
          DBPhi = 70;
      } else if (weight > 9 && weight <= 11) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 11 && weight <= 13) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 13 && weight <= 15) {
        var HRlow = 80,
          HRhi = 130,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 15 && weight <= 17) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 17 && weight <= 19) {
        var HRlow = 80,
          HRhi = 120,
          RRlow = 20,
          RRhi = 30,
          SBPlow = 80,
          SBPhi = 110,
          DBPlow = 50,
          DBPhi = 80;
      } else if (weight > 19 && weight <= 21) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 21 && weight <= 23.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 23.5 && weight <= 26.5) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 26.5 && weight <= 30) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 30 && weight <= 34) {
        var HRlow = 70,
          HRhi = 110,
          RRlow = 15,
          RRhi = 30,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 34 && weight <= 38) {
        var HRlow = 60,
          HRhi = 105,
          RRlow = 15,
          RRhi = 20,
          SBPlow = 85,
          SBPhi = 120,
          DBPlow = 55,
          DBPhi = 80;
      } else if (weight > 38 && weight <= 49) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 49 && weight <= 69) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight > 69 && weight <= 99) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      } else if (weight >= 100) {
        var HRlow = 60,
          HRhi = 100,
          RRlow = 12,
          RRhi = 20,
          SBPlow = 80,
          SBPhi = 120,
          DBPlow = 60,
          DBPhi = 80;
      }
      // Place observations into the table
      document.getElementById("HRlow").innerHTML = HRlow;
      document.getElementById("HRhi").innerHTML = HRhi;
      document.getElementById("RRlow").innerHTML = RRlow;
      document.getElementById("RRhi").innerHTML = RRhi;
      document.getElementById("SBPlow").innerHTML = SBPlow;
      document.getElementById("SBPhi").innerHTML = SBPhi;
      document.getElementById("DBPlow").innerHTML = DBPlow;
      document.getElementById("DBPhi").innerHTML = DBPhi;
    }

大幅削减JS。

  • TAG:
相关文章
我们已经准备好了,你呢?
2024我们与您携手共赢,为您的企业形象保驾护航