我想知道为什么我的数组不起作用。难道我做错了什么?我一直在尝试很多不同的阵列但没有工作。我只是错过了一些东西吗?
费用系统: 编写一个程序,询问用户的差旅费用。它应该询问费用名称,然后是金额。它应该继续收取费用,直到用户将输入留空。
任何100美元及以下的费用都应自动获得批准。 任何超过100美元的费用都不应自动批准。
总计批准和未批准的费用。
首先显示已批准的费用。然后显示需要手动批准的费用。
显示已批准的总费用和总“需求批准”费用。
提前致谢!
//Variables
var expenseName;
var expensePrice;
var loopAgain = true;
var approvedExpenses = new Array();
var approvedExpensePrice = new Array();
var unApprovedExpenses = new Array();
var unApprovedExpensePrice = new Array();
var approvedExpenseTotal = 0;
var unApprovedExpenseTotal = 0;
while(loopAgain) {
expenseName = prompt("What is the expense? - Type 'done' to end.");
expensePrice = prompt("What is the price?");
expensePrice = Number(expensePrice);
if (expenseName === "done"){
loopAgain = false;
}
for(var count = 0; count < approvedExpenses.length; count++){
if (expensePrice < 100) {
approvedExpenseTotal = approvedExpenseTotal + expensePrice;
approvedExpenses[count] = expenseName;
approvedExpensePrice[count] = expensePrice;
}else {
unApprovedExpenseTotal = unApprovedExpenseTotal + expensePrice;
unApprovedExpenses[count] = expenseName;
unApprovedExpensePrice[count] = expensePrice;
}
}
}
document.write("APPROVED EXPENSES:<br>");
for(var count = 0; count < approvedExpenses.length; count++){
document.write(approvedExpenses[count] + ": ");
document.write(approvedExpenses[count] + "\n");
}
document.write("EXPENSES NEEDING MANUAL APPROVAL:<br>");
for(var count = 0; count < unApprovedExpenses.length; count++){
document.write(unApprovedExpenses[count] + ": ");
document.write(unApprovedExpenses[count] + "\n");
}
document.write("TOTALS:<br>");
document.write("Total approved expenses: $" + approvedExpenseTotal + "<br>");
document.write("Total expenses waiting approval: $" + unApprovedExpenseTotal + "<br>");
答案 0 :(得分:1)
你的程序逻辑有点奇怪。 = b关于为什么没有显示任何内容的具体问题是您的数组是空的。
它们是空的,因为你的for循环没有运行。
你的for循环没有运行,因为for循环中的条件基于“approvedExpenses.length
”,它总是为零。
尝试从上到下阅读代码(执行流程)并描绘变量的所有值。它应该揭示approvedExpenses.length
为零的原因。
(也在你的for循环中,你设置值的方式可能会最终覆盖现有值,因为计数器总是从0开始,可能存在现有数据)