javascript - 显示所需的并行阵列范围
问题描述
我对编码比较陌生,需要一点帮助来解决家庭作业问题。我在这个问题上已经走得很远了,但我很难在输出中只显示所需的范围。我的代码运行良好,但它并没有给我所需的输出。我将把我的代码放在问题下面。谢谢你!
输入存储在并行数组中的员工姓名和薪水列表。工资应为以 100 为增量的浮点数。例如,36,000 美元的工资应输入为 36.0,85,900 美元的工资应输入为 85.9。找出平均(平均)工资,并显示收入在平均值 5,000 美元范围内的员工的姓名和工资。换言之,如果平均工资为 45,000 美元,则应显示所有收入在 40,000 美元到 50,000 美元之间的员工。注意:场景中使用的值用于演示目的;程序中使用的值应基于浮点数据类型和 100 的增量。
// Declare and initialize variables
var BR = "<br>"
var Name = []
var Sal = []
var Index = 0
var Sum = 0
var AverageSal = 0
var SP = " "
// Priming read
Name[Index] = prompt("Enter an employee name, or * to quit: ")
// Loop for more input until user enters * for employee name
while (Name[Index] != "*") {
Sal[Index] = parseFloat(prompt("Enter the salary for that employee in increments of 100: "))
Index++
Name[Index] = prompt("Enter another employee name, or * to quit: ")
}
// Find the average Sal
for(Index = 0; Index < Sal.length; Index++) {
Sum += Sal[Index];
}
AverageSal = Sum / Sal.length;
document.write(AverageSal + BR)
// Display desired output
RangeMin = AverageSal -= 5
RangeMax = AverageSal += 5
Index = 0
while (Name[Index] != "*") {
if (Sal[Index] >= RangeMin || Sal[Index] <= RangeMax) {
document.write("Employee: " + Name[Index] + SP + "Sal: " + Sal[Index] + BR)
}
Index++
}
document.write(RangeMin + SP + RangeMax)
解决方案
推荐阅读
- wordpress - React Native App with WordPress REST API - 不允许以该用户的身份创建帖子
- c# - 在文本框中输入值时如何显示两位小数?
- java - 没有可用于离线模式的 com.android.tools.build:aapt2:3.2.0-alpha18-4804415 缓存版本
- python - Django Rest Framework ViewSet 不按字段过滤
- java - 如何在 Theamleaf 中编写 String.Contains
- php - 如何在 PHP 中访问字符串中的值
- asp.net - vb.net DataGridview 比较
- apache-camel - 如何从 apache camel 客户端强制执行 TLSv1.2
- powershell - 根据列值将 CSV 文件拆分为多个文件
- google-apps-script - 安装私人 Google 表格插件