In class group practice WOD: BK Menu

Divide up into teams of two. You will each complete this WOD on your own computer and create your own JSFiddle to hold your work, but you must work “synchronously”–i.e. both of you must type each line of code or perform each action at the same time, and thus you will both complete this WOD at the same time. This means you must talk to each other continuously about what you are doing.

The goal of this WOD is to implement a class that keeps track of the total calories, fat, and sodium in a Burger King order and notifies you when your order has exceeded the recommended daily maximum for a sedentary college student (i.e. 2,000 calories, 70 grams fat, 2300 mg sodium).

Here is some code to define objects containing reference values for various BK items:

var bkItems = {
  cokeLarge:     {calories: 380, fat: 0, sodium: 10},
  cokeMedium:    {calories: 290, fat: 0, sodium: 5},
  cokeSmall:     {calories: 140, fat: 0, sodium: 0},
  frappeLarge:   {calories: 600, fat: 25, sodium: 360},
  frappeSmall:   {calories: 410, fat: 19, sodium: 230},
  whopper:        {calories: 650, fat: 37, sodium: 910},
  doubleWhopper: {calories: 900, fat: 56, sodium: 980},
  tripleWhopper: {calories: 1160, fat: 75, sodium: 1050},
  chickenSandwich: {calories: 660, fat: 40, sodium: 1220},
  friesLarge:    {calories: 500, fat: 22, sodium: 900},
  friesMedium:   {calories: 410, fat: 18, sodium: 570},
  friesSmall:    {calories: 240, fat: 10, sodium: 330}

Now create a class called BKMenu whose constructor accepts the bkItems data structure and saves it. The BKMenu class should also store the recommended daily maximum for calories, fat, and sodium.

BKMenu implements an add() function that accepts a string indicating the item ordered, which should be one of the fields in the bkItems data structure. For example, “cokeLarge”, “whopper”.

Whenever the add() function is called, if the recommended daily maximum for calories, fat, or sodium is now exceeded for that order, the function should print out the total along with a message to the console for that value.

For example:

const myMenu = new BKMenu(bkItems);
// console prints:
//   Calories exceeds allowance of 2000: 2140
//   Fat exceeds allowance of 70: 100
// console prints:
//   Calories exceeds allowance of 2000: 2640
//   Fat exceeds allowance of 70: 122
//   Sodium exceeds allowance of 2300: 2320

Ready? Let’s begin:

  1. Login to JSFiddle.

  2. Copy and paste the bkItems variable definition into JSFiddle so that data is available.

  3. Create a Javascript class called “BKMenu”. It provides an add() function that takes a string and updates internal counters appropriately. When thresholds for calories, fat, or sodium are exceeded, it prints out a message to the console.

  4. Informally test your program by running it and inspecting the output.

  5. When you are confident the function works correctly, press “Save” to create a URL to your JSFiddle.

  6. Raise your hand to let me know you have finished.

Rx: < 8 min Av: 8-12 min Sd: 12-15 min DNF: 15+ min

Submission instructions

We will go over the solution to this problem in class.

This page is also available via