难以通过函数调用进行迭代 - 第一个 Class 个函数

Difficulty Iterating Through Function Call - First Class Functions

我提供的代码可以正确执行,但是正如您将看到的那样,它会在转移到下一位客人之前反复为每位客人提供点心。

我绞尽脑汁想知道如何以一种有效的方式修改我的代码,以便轮流为每位客户提供茶点,但当程序 运行.

非常感谢所有建议。

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


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

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

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

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

这与您的原始代码一样高效。

这可能是我清理东西并更好地组织它的方式。

基本上,这为您提供了两个功能 - 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);

您也可以在此代码笔中看到它 运行: https://codepen.io/barneychampaign/pen/oNzNOXv