获得最小和最大

时间:2018-11-23 20:56:37

标签: javascript

编写一个访问员工工资记录集的程序。该程序将确定员工的平均,最高和最低时薪。输出结果格式如示例输出中所示。

使用函数编写该程序,以将程序分为多个模块。主要功能应主要由函数调用以及WHILE循环组成。在这种情况下,主函数中的多个IF语句是如此简单,以至于它们只能在主函数中完全处理。

最大值应等于30 最小值应等于0

我无法显示这些结果。 无法对这些数字进行硬编码,必须通过打开记录来确定。

这是我的代码:

const START_VALUE = 0;
var records;
var counter;
var hourlyWage;
var grandSumHourlyWages;
var minimumHourlyWage;
var averageHourlyWage;
var maximumHourlyWage;


function assignInitialValuesToVariables() {
"use strict";

counter = START_VALUE;
hourlyWage = START_VALUE;
grandSumHourlyWages = START_VALUE;
averageHourlyWage = START_VALUE;
minimumHourlyWage = START_VALUE;
maximumHourlyWage = START_VALUE;
}

// purpose of this function is to calculate the average
function calculateAverageHourlyWage() {
averageHourlyWage = grandSumHourlyWages / counter;
}

// purpose of this function is to display the results
function displayMinimumMaximumAverageHourlyWage() {
    document.write("Average Hourly Wage: " + averageHourlyWage + "<br/>"
    + "Maximum Hourly Wage: " + maximumHourlyWage + "<br/>"
    + "Minumum Hourly Wage: " + minimumHourlyWage);
}

function lab12puttingItAllTogetherPart2() {
"use strict";

// A suggested main function for Part 2 is below:

assignInitialValuesToVariables();

records = openEmployeePayrollRecords();          // pre-existing function

while ( records.readNextRecord() ) {
    hourlyWage = records.getEmployeeHourlyWage();   //pre-existing method
    grandSumHourlyWages += hourlyWage;    // for later use in calc avg wage
    counter++;

    if (hourlyWage < minimumHourlyWage) {
        minimumHourlyWage = hourlyWage;
    }


    // here write the IF to handle max hourly wage
    if (hourlyWage > maximumHourlyWage) {
        maximumHourlyWage = hourlyWage;
    }

}

calculateAverageHourlyWage();

displayMinimumMaximumAverageHourlyWage();

}

0 个答案:

没有答案