gpt4 book ai didi

Javascript 检查记录集中的最大/最小/平均数

转载 作者:行者123 更新时间:2023-11-28 02:48:50 25 4
gpt4 key购买 nike

function lab09unknownLoopCountPart2() {

//Constants
var ONE_HUNDRED = 100;
var ZERO = 0;

//Variables
var employeeHourlyWage;
var hourlyMaximum = ZERO;
var hourlyMinimum = ZERO;
var totalHourlyWage = ZERO;
var records;
var counter = ZERO;
var average;

records = openEmployeePayrollRecords();

while (records.readNextRecord()) {
employeeHourlyWage = records.getEmployeeHourlyWage();
totalHourlyWage += employeeHourlyWage;
counter++;
average = totalHourlyWage / counter;

if (employeeHourlyWage < ONE_HUNDRED) {
hourlyMaximum = employeeHourlyWage;
}
if (employeeHourlyWage < ONE_HUNDRED) {
hourlyMinimum = employeeHourlyWage;
}
}
document.write("Average Hourly Wage: " + average + "<br />");
document.write("Maximum Hourly Wage: " + hourlyMaximum + "<br />");
document.write("Minimum Hourly Wage: " + hourlyMinimum + "<br />");

}

记录集只是 15 - 30 之间的数字。我必须对它们进行排序并找到最大值、最小值和平均值。

我知道我的两个 IF 语句不正确,我想知道如何使用 Math.min 和 Math.max 来检查这一点。

谢谢。

最佳答案

   var hourlyMinimum = -1;
var hourlyMaximum = -1;

if (hourlyMaximum === -1 || employeeHourlyWage > hourlyMaximum ) {
hourlyMaximum = employeeHourlyWage;
}
if (hourlyMinimum === -1 || employeeHourlyWage < hourlyMinimum ) {
hourlyMinimum = employeeHourlyWage;
}

假设工资不能为负。

关于Javascript 检查记录集中的最大/最小/平均数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4260362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com