0

The code I have provided executes properly, however as you will see it offers refreshments to each guest repeatedly before moving on to the next guest.

I'm scratching my head as to how I can alter my code, in an efficient way, so that each customer is offered refreshments in turn, but still attended to four times each when the program is run.

All suggestions are greatly appreciated.

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


2
  • Can you explain more? Commented Nov 27, 2020 at 16:02
  • 1
    What exactly is the desired outcome here? Where do higher order functions come in? Commented Nov 27, 2020 at 16:02

3 Answers 3

2

It's hard to make a recommendation without knowing more about what you're trying to do here. If you're just trying to run this small program in isolation, then one approach would be to just call your getRefreshmentOrder once in serveGuest, and then add a loop at the end to serve all guests four times. You'll then serve each guest once in order, and repeat that again three more times.

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

That's no more or less efficient than your original code.

Sign up to request clarification or add additional context in comments.

Comments

1

you could wrap this in another for loop that runs four times and then only call getRefreshmentOrder() one time in your serveGuest function

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

Comments

0

This is probably how I would do it to clean things up and organize it a little better.

Basically, this gives you two functions - takeOrder(guest) and takeOrders(guests), so you can either serve a single guest by just calling the former, or serve an array of guests using the latter. Either way, they both boil down to calling the takeOrder function on an instance of a guest, so you can just update that one function to do whatever it needs to do and you’re covered everywhere.

It also allows you to change the number of times you’re going to serve them by updating the value in the totalNumberOfTimesToServe constant.

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

You can see it running in this codepen, too: https://codepen.io/barneychampaign/pen/oNzNOXv

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.