我提供的代码可以正常执行,但是正如您所看到的,它会反复为每位客人提供茶点,然后再继续下一位客人。
我正在摸索如何以一种有效的方式更改我的代码,以便轮流为每个客户提供茶点,但在程序运行时仍然每次都要参加四次。
非常感谢所有建议。
JS:
var guests = [
{name: "Rick Sanchez", paid: false, loyaltyCard: true},
{name: "Morty Smith", paid: true, loyaltyCard: true},
{name: "Beth Smith", paid: true, loyaltyCard: false},
{name: "Jerry Smith", paid: true, loyaltyCard: false},
{name: "Sleepy Gary", paid: true, loyaltyCard: false},
{name: "Summer Smith", paid: true, loyaltyCard: false},
{name: "Mr. Poopybutthole", paid: true, loyaltyCard: true},
{name: "Pencilvester", paid: true, loyaltyCard: false}
];
function serveGuest(guest) {
var getRefreshmentOrder = createRefreshmentOrder(guest);
getRefreshmentOrder();
// Loyalty Stamps
getRefreshmentOrder();
getRefreshmentOrder();
// Agressive Advertisment
getRefreshmentOrder();
// Thank you. Come again.
}
function createRefreshmentOrder(guest) {
var orderFunction;
if (guest.loyaltyCard) {
orderFunction = function() {
alert("Would you like any premium refreshments from our Membership Menu, at no extra cost?");
};
} else {
orderFunction = function() {
alert("Can we get you any refreshments?");
};
}
return orderFunction;
}
function serveAllGuests(guests) {
for (var i = 0; i < guests.length; i++) {
serveGuest(guests[i]);
}
}
serveAllGuests(guests);