在 table javascript 中的最小值旁边显示 *
Showing an * next to smallest value in a table javascript
我正在尝试在 table 中找到最小的油耗值。我不确定如何找到它。
我有一个用户输入了 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> </td>");
}
}
在您的方法中,每次向 VehicleArray
添加一个元素时,您都在检查该元素与之前添加的元素相比是否是最小值,如果是,则直接将其标记为一个星号。
然而,可能有一些尚未添加的元素具有更小的值。但是由于for循环还没有到达这些,当前元素是标有星号。
那么在下一次迭代中,如果下一辆车的油耗更低,也会用星号标记,以此类推。
解决这个问题的方法是使用两个for循环:一个用于填充数组并获取最小值,另一个用于显示[=33=中的元素].
//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> </td>");
}
}
另外,附带说明一下,你为什么要这样做?
MostEfficient = LowestConsumption;
MostEfficient = x;
可能是错误的变量名?否则第一个语句没用
您不能在单个循环中执行此操作,因为在用户输入之前您不知道它。
将算法分成两个循环应该更好:一个填充 VehicleArray
,另一个显示 table。在这两者之间,您需要找到 MostEfficient
.
因为看起来像是作业,所以我不会给你代码作为答案,但我可以图解:
loop:
-> fetching data and filling the array
-> finding the most efficient
-> displaying the table
我正在尝试在 table 中找到最小的油耗值。我不确定如何找到它。 我有一个用户输入了 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> </td>");
}
}
在您的方法中,每次向 VehicleArray
添加一个元素时,您都在检查该元素与之前添加的元素相比是否是最小值,如果是,则直接将其标记为一个星号。
然而,可能有一些尚未添加的元素具有更小的值。但是由于for循环还没有到达这些,当前元素是标有星号。
那么在下一次迭代中,如果下一辆车的油耗更低,也会用星号标记,以此类推。
解决这个问题的方法是使用两个for循环:一个用于填充数组并获取最小值,另一个用于显示[=33=中的元素].
//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> </td>");
}
}
另外,附带说明一下,你为什么要这样做?
MostEfficient = LowestConsumption;
MostEfficient = x;
可能是错误的变量名?否则第一个语句没用
您不能在单个循环中执行此操作,因为在用户输入之前您不知道它。
将算法分成两个循环应该更好:一个填充 VehicleArray
,另一个显示 table。在这两者之间,您需要找到 MostEfficient
.
因为看起来像是作业,所以我不会给你代码作为答案,但我可以图解:
loop:
-> fetching data and filling the array
-> finding the most efficient
-> displaying the table