0

我提供的代码可以正常执行,但是正如您所看到的,它会反复为每位客人提供茶点,然后再继续下一位客人。

我正在摸索如何以一种有效的方式更改我的代码,以便轮流为每个客户提供茶点,但在程序运行时仍然每次都要参加四次。

非常感谢所有建议。

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);


4

3 回答 3

2

如果不了解您在此处尝试做什么,就很难提出建议。如果你只是想单独运行这个小程序,那么一种方法是只调用你的getRefreshmentOrderonce in serveGuest,然后在最后添加一个循环来为所有客人服务四次。然后,您将按顺序为每位客人服务一次,然后再重复三遍。

function serveGuest(guest) {
    var getRefreshmentOrder = createRefreshmentOrder(guest);
    getRefreshmentOrder();
}

function serveAllGuests(guests) {
    for (var i = 0; i < guests.length; i++) {
        serveGuest(guests[i]);
    }
}

for (var i = 0; i < 4; i++) {
  serveAllGuests(guests)
}

这并不比您的原始代码效率更高或更低。

于 2020-11-27T16:08:06.460 回答
1

您可以将其包装在另一个运行四次的 for 循环中,然后在您的 serveGuest 函数中只调用一次 getRefreshmentOrder()

function serveAllGuests(guests) {
    for(var j = 0; j < 4; j++){
        for (var i = 0; i < guests.length; i++) {
            serveGuest(guests[i]);
        }
    }
}
于 2020-11-27T16:06:43.377 回答
0

这可能是我将如何清理并更好地组织它的方式。

基本上,这为您提供了两个功能 -takeOrder(guest)takeOrders(guests),因此您可以通过调用前者为单个客人提供服务,或者使用后者为一组客人提供服务。无论哪种方式,它们都归结为takeOrder在来宾实例上调用该函数,因此您只需更新该函数以执行它需要执行的任何操作,并且您无处不在。

它还允许您通过更新totalNumberOfTimesToServe常量中的值来更改为它们服务的次数。

const 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 }
];
const totalNumberOfTimesToServe = 4;

var timesServed = 0;

function takeOrder(guest) {
  if (guest.loyaltyCard) {
    console.log(
      `Would you like any premium refreshments from our Membership Menu, at no extra cost, ${guest.name}?`
    );
  } else {
    console.log(`Can we get you any refreshments, ${guest.name}?`);
  }
}

function takeOrders(guests) {
  // take orders for each guest until we've fulfilled our iteration count
  while (timesServed < totalNumberOfTimesToServe) {
    console.log(`Serving number: ${timesServed + 1}`);

    guests.forEach((guest) => {
      takeOrder(guest);
    });

    // after each iteration of taking orders, increment our times served by one
    timesServed++;
  }

  // reset our times served back to 0
  timesServed = 0;
}

takeOrders(guests);

你也可以看到它在这个 codepen 中运行: https ://codepen.io/barneychampaign/pen/oNzNOXv

于 2020-11-27T16:27:05.923 回答