【问题标题】:Showing an * next to smallest value in a table javascript在表格javascript中的最小值旁边显示*
【发布时间】:2020-01-16 13:35:00
【问题描述】:

我试图在我的表格中找到最小的油耗值。我不确定如何找到它。 我有一个用户输入了 5 辆车的数量。收集点是一个具有 5 个属性的构造函数的对象。然后我将五个对象的详细信息存储在一个由 5 个对象组成的数组中。 当我尝试编写用于获取最低数字的代码时,我将我的显示器作为第一个带有 * 的显示器,第二个带有 * 的显示器,第二个最低的显示器作为 *。最小值之后的所有值都是空白的。我只想要最低的作为*。下面是我让用户输入 5 次后的代码。所有这些都在一个 for 循环中。其中一个属性计算原始对象构造函数中的消耗。

for (var x = 1; x <= 5; x++) {
  registrationPlateNumber = prompt("Please enter a 6 character registration number.", "");
  while (String(registrationPlateNumber).length != 6) {
    registrationPlateNumber = prompt("Invalid registration number! Please enter a 6 character registration number.", "");
  }

  fuelTankVolume = prompt("Please enter the volume of the vehicle's fuel tank in litres.", "");
  fuelTankVolume = parseInt(fuelTankVolume);
  while (isNaN(fuelTankVolume) || fuelTankVolume <= 0) {
    fuelTankVolume = prompt("Invalid volume! Please enter the volume of the vehicle's fuel tank in litres.", "");
    fuelTankVolume = parseInt(fuelTankVolume);
  }

  distanceTravelled = prompt("Please enter the distance the vehicle can travel on a full tank of fuel.", "");
  distanceTravelled = parseInt(distanceTravelled);
  while (isNaN(distanceTravelled) || distanceTravelled <= 0) {
    distanceTravelled = prompt("Invalid distance! Please enter the distance the vehicle can travel on a full tank of fuel.", "");
    distanceTravelled = parseInt(distanceTravelled);
  }

  VehicleArray[x] = new Vehicle(registrationPlateNumber, fuelTankVolume, distanceTravelled);

  document.writeln("<tr>");
  document.writeln("<td>" + VehicleArray[x].registrationPlateNumber + "</td>");
  document.writeln("<td>" + VehicleArray[x].fuelTankVolume + "</td>");
  document.writeln("<td>" + VehicleArray[x].distanceTravelled + "</td>");
  document.writeln("<td>" + VehicleArray[x].fuelConsumption + "</td>");

  if (VehicleArray[x].fuelConsumption < LowestConsumption) {
    LowestConsumption = VehicleArray[x].fuelConsumption;
    MostEfficient = LowestConsumption;
    MostEfficient = x;
  }

  if (x == MostEfficient) {
    document.writeln("<td>*</td>");
  }
  else if (x != MostEfficient) {
    document.writeln("<td>&nbsp;</td>");
  }
}

【问题讨论】:

    标签: javascript arrays min


    【解决方案1】:

    在您的方法中,每次向VehicleArray 添加元素时,您都在检查该元素与之前添加的元素相比是否最小,如果是,则直接用星号标记它。

    但是,可能还有一些尚未添加的元素具有较小的值。但由于 for 循环尚未到达这些元素,当前元素标有星号。

    那么在下一次迭代中,如果下一辆车的油耗更低,也会用星号标记,以此类推。

    解决此问题的方法是使用两个 for 循环:一个用于填充数组并获取最小值,另一个用于显示表格中的元素。

     //first loop: add all elements to the array and get the index of the minimum
    
     var MostEfficient = 0;
     for (var x = 1; x <= 5; x++){
        //code for user prompts...
    
        //adding the vehicle to the array
         VehicleArray[x] = new Vehicle(registrationPlateNumber, fuelTankVolume, distanceTravelled);
    
         //checking if it is the minimum for now; only the final minimum will be used in the next loop
         if (VehicleArray[x].fuelConsumption < LowestConsumption){
             LowestConsumption = VehicleArray[x].fuelConsumption;
              MostEfficient = x;
             }
    
    }
    
    //second for loop to write the elements to the document
    //now MostEfficient won't update anymore, and will point to the final minimum
    
    for(var x = 1; x <= 5; x++)
        document.writeln(  "<tr>");
        document.writeln(   "<td>" + VehicleArray[x].registrationPlateNumber + "</td>");
        document.writeln(   "<td>" + VehicleArray[x].fuelTankVolume + "</td>");
        document.writeln(   "<td>" + VehicleArray[x].distanceTravelled + "</td>");
        document.writeln(   "<td>" + VehicleArray[x].fuelConsumption + "</td>");
    
        if (x == MostEfficient){
             document.writeln("<td>*</td>");
        }
         else if (x != MostEfficient){
             document.writeln("<td>&nbsp;</td>");
         }
    }
    

    另外,顺便说一句,你为什么要这样做?

    MostEfficient = LowestConsumption;
    MostEfficient = x;
    

    可能是错误的变量名?否则第一个语句是没有用的。

    【讨论】:

    • 感谢您澄清这两个循环。我想我可能不得不这样做,但不确定。现在可以开始工作了。
    • @TheBear 很高兴这有帮助!如果这解决了您的问题,请考虑将投票/标记为答案。
    【解决方案2】:

    您不能在单个循环中执行此操作,因为在给出用户输入之前您不知道它。

    将算法分成两个循环应该更好:一个填满VehicleArray,另一个显示表格。在这两者之间你需要找到MostEfficient

    因为它看起来像家庭作业,所以我不会给你代码作为答案,但我可以图解:

    loop:
      -> fetching data and filling the array
    
    -> finding the most efficient
    
    -> displaying the table
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 2020-02-20
      • 2015-09-17
      • 2015-07-04
      • 1970-01-01
      相关资源
      最近更新 更多