<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
//Definition of staff members (class)
function StaffMember(name,discountPercent){
this.name = name;
this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally",5);
var b122 = new StaffMember("Bob",10);
var d123 = new StaffMember("Dave",20);
staffMembers = [s121,b122,d123];
//Creation of cash register (object)
var cashRegister = {
total:0,
lastTransactionAmount: 0,
//Add to the total (method)
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
//Retreive the value of an item (method)
scan: function(item,quantity){
switch (item){
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
return true;
},
//Void the last item (method)
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
//Apply a staff discount to the total (method)
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.discountPercent / 100);
}
};
//Ask for number of items
do {
var numOfItems = prompt("How many items do you have?");
document.body.innerHTML = numOfItems;
if (isNaN(numOfItems)) {
i=0;
} else {
i=1;
}
} while (i===0);
//Ask for item and qty of item
var items = [];
var qtys = [];
for(var i=0;i<numOfItems;i++) {
var j=0;
do {
items[i] = prompt("What are you buying? (eggs, milk, magazine, chocolate)");
switch (items[i]) {
case "eggs" :;
case "milk" :;
case "magazine" :;
case "chocolate" : j=1; document.body.innerHTML = items[i]; break;
default : document.body.innerHTML = 'Item not reconized, please re-enter...'
; break;}
} while (j===0);
do {
qtys[i] = prompt("How many " + items[i] + " are you buying?");
document.body.innerHTML = qtys[i];
if (isNaN(qtys[i])) {
j=1;
} else {
j=0;
}
} while (j===1);
//Add to the sub-total
cashRegister.scan(items[i],qtys[i])
}
//Find out if it's a staff member & if so apply a discount
var customer;
var staffNo;
do {
customer = prompt("Please enter customer name or type 'staff'.");
document.body.innerHTML = customer;
if (customer === 'staff') {
staffNo = prompt("Please enter your staff number");
for (i in staffMembers) {
if (staffMembers[i] === staffNo) {
cashRegister.applyStaffDiscount(staffNo);
} else {
document.body.innerHTML = "Staff number not found";
};
}
}
i=1;
} while (i=0);
// Show the total bill
if (customer !== 'staff') {
document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
+' Thank you for visiting ' +customer;
} else {
document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
+' Thank you for visiting ' +staffNo;
};
</script>
</body>
</html>
我的代码似乎有问题,它可以工作,但不适用员工折扣,我觉得错误就在附近;
for (i in staffMembers) {
if (staffMembers[i] === staffNo) {
cashRegister.applyStaffDiscount(staffNo);
} else {
document.body.innerHTML = "Staff number not found";
};
}
任何人都可以帮助发现错误吗?我一直在 CodeAcademy 上学习,但通过检查输入的数据进一步采用了最后一个示例。但我似乎不明白为什么此部分在通过“http://www.compileonline.com/try_javascript_online.php”检查时无法正常工作。 '.
最佳答案
事实证明你的代码有很多错误。要使其“正常工作”,您需要修复的关键问题如下:
首先 - 您要求提供“员工编号”,但您的员工结构没有足够的空间。您可以按如下方式修改 StaffMember
:
//Definition of staff members (class)
function StaffMember(name, number, discountPercent){
this.name = name;
this.number = number;
this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally","s121",5);
var b122 = new StaffMember("Bob","b122",10);
var d123 = new StaffMember("Dave","d123",20);
staffMembers = [s121,b122,d123];
现在您有了一个“唯一标识符” - 我决定为员工提供与变量名称相同的编号,但这不是必需的。
接下来,让我们看看您的循环:将其更改为
do {
customer = prompt("Please enter customer name or type 'staff'.");
document.body.innerHTML = customer;
if (customer === 'staff') {
staffNo = prompt("Please enter your staff number:");
for (i in staffMembers) {
if (staffMembers[i].number === staffNo) { // <<<<< change the comparison
cashRegister.applyStaffDiscount(staffMembers[i]); // <<<<< call applyStaffDiscount with the right parameter: the object, not the staff number
} else {
document.body.innerHTML = "Staff number not found";
};
}
}
i=1; // <<<<< I really don't understand why you have this do loop at all.
} while (i == 0); // <<<<< presumably you meant "while(i == 0)"? You had "while (i=0)"
您可以做很多很多事情来改善这一点 - 但至少这会让您开始。完整的“工作”代码(我可能做了其他我忘记指出的编辑 - 但下面的内容是直接从我的工作空间复制的,并且“有效” - 尽管相当不稳定):
<!DOCTYPE html>
<html>
<body>
<script language="javascript" type="text/javascript">
//Definition of staff members (class)
function StaffMember(name, number, discountPercent){
this.name = name;
this.number = number;
this.discountPercent = discountPercent;
}
//Creation of staff members (object)
var s121 = new StaffMember("Sally","s121",5);
var b122 = new StaffMember("Bob","b122",10);
var d123 = new StaffMember("Dave","d123",20);
staffMembers = [s121,b122,d123];
//Creation of cash register (object)
var cashRegister = {
total:0,
lastTransactionAmount: 0,
//Add to the total (method)
add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;
},
//Retreive the value of an item (method)
scan: function(item,quantity){
switch (item){
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45 * quantity); break;
}
return true;
},
//Void the last item (method)
voidLastTransaction : function(){
this.total -= this.lastTransactionAmount;
this.lastTransactionAmount = 0;
},
//Apply a staff discount to the total (method)
applyStaffDiscount: function(employee) {
this.total -= this.total * (employee.discountPercent / 100);
}
};
//Ask for number of items
do {
var numOfItems = prompt("How many items do you have?");
document.body.innerHTML = numOfItems;
if (isNaN(numOfItems)) {
i=0;
} else {
i=1;
}
} while (i===0);
//Ask for item and qty of item
var items = [];
var qtys = [];
for(var i=0;i<numOfItems;i++) {
var j=0;
do {
items[i] = prompt("What are you buying? (eggs, milk, magazine, chocolate)");
switch (items[i]) {
case "eggs" :;
case "milk" :;
case "magazine" :;
case "chocolate" : j=1; document.body.innerHTML = items[i]; break;
default : document.body.innerHTML = 'Item not reconized, please re-enter...'
; break;}
} while (j===0);
do {
qtys[i] = prompt("How many " + items[i] + " are you buying?");
document.body.innerHTML = qtys[i];
if (isNaN(qtys[i])) {
j=1;
} else {
j=0;
}
} while (j===1);
//Add to the sub-total
cashRegister.scan(items[i],qtys[i])
}
//Find out if it's a staff member & if so apply a discount
var customer;
var staffNo;
do {
customer = prompt("Please enter customer name or type 'staff'.");
document.body.innerHTML = customer;
if (customer === 'staff') {
staffNo = prompt("Please enter your number:");
for (i in staffMembers) {
if (staffMembers[i].number === staffNo) {
cashRegister.applyStaffDiscount(staffMembers[i]);
} else {
document.body.innerHTML = "Staff number not found";
};
}
}
i=1;
} while (i=0);
// Show the total bill
if (customer !== 'staff') {
document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
+'<br> Thank you for visiting ' + customer;
} else {
document.body.innerHTML = 'Your bill is £'+cashRegister.total.toFixed(2)
+'<br> Thank you for visiting staff member ' +staffNo;
};
</script>
</body>
</html>
关于javascript - 我的 javascript for-in 循环出了什么问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584637/