0

I'm working on a simple registration system which displays the number of seats available in a particular workshop. It's built in Google Apps Script which runs with a JavaScript syntax.

I build an array of Class objects for the user when they log in. One of the object keys is seats, which shows the available number of spots left in the workshop. To get that, I compare the master list of classes to get the max number of registrants to the current registration list.

Class Object

[{ 
  date: 4/10/2017,
  title: "Workshop 1",
  desc: "A string description for 1",
  seats: ""
}]

Google Sheets table

| date      | title      | description                | seats |   |
|-----------|------------|----------------------------|-------|---|
| 4/10/2017 | Workshop 1 | A string description for 1 | 20    |   |
| 5/10/2017 | Workshop 2 | A string description for 2 | 25    |   |

Current Registrations

| user  | class0    | class1    |
|-------|-----------|-----------|
| user1 | 4/10/2017 |           |
| user2 | 4/10/2017 | 5/10/2017 |
|       |           |           |

Script

// Set the count variable
  var count;

  // Sessions the user is not registered for
  for(var i=0; i<sessions.length; i++) {

    // Look the class up in the master list
    for(var j=0; j<allSessionsData.length; j++) {

      // Find the stored date in the master list
      var date = allSessionsData[j][0];

      // match the session dates to find the max seats
      if(sessions[i].date === date) {
        sessions[i].seats = allSessionsData[j][5];
      } 
    }
  }

  // Reopen the sessions loop to get the current counts
  for(var i=0; i<sessions.length; i++) {
    var count = sessions[i].seats;

    // Get the current 2D registrations array
    for(var j=0; j<allRegsData.length; j++) {
      for(var k=0; k<allRegsData[j].length; k++) {

        if(sessions[i].date === allRegsData[j][k]) {
          count--;
          sessions[i].seats = count;
        }
      }
    }
  }
  // Return the updated array
  return sessions;
}

The function would return 18 for the 4/10 date and 24 for the 5/10. The script is working but I'm wondering if this should be condensed into one loop and why. I know "best practice" is subjective, but it feels redundant to build one array only to reopen it immediately within the same function.

2
  • adding data sample will help Commented Mar 28, 2017 at 12:56
  • 1
    You might move sessions[i].seats = count; out of the double-loop and place it before the end of the main loop. Or you could do without the count and use --sessions[i].seats; Commented Mar 28, 2017 at 12:58

2 Answers 2

1

Here's how I would refactor it and this is definitely subjective, but I like it using es6 cause it's easy on the eyes.

function refactor(sessions) {

  // Sessions the user is not registered for
  sessions.forEach(session => {

    // update seats based on allSessionsData
    allSessionsData.forEach(sessionData => {
      if (session.date === sessionData[0]) {
        sessions.seats = sessionData[5];
      }
    });

    // update seats based on allRegsData
    allRegsData.forEach(allRegs => {
      allRegs.forEach(reg => {
        if (session.date === reg) {
          session.seats--;
        }
      });
    });

  });

  return sessions;
}

So I removed the for loops and used foreach because for me personally it makes it easier to think about the problem if I'm not focusing on index. Also since you are updating each value of session again you can keep it in the first loop. From what I understand all you really want to do is get seat data and then the count based on another collection of data (I could be wrong I haven't tested or ran this code with any data) .

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

Comments

0

The user classes table has a bad design, since you cannot easily accomodate users who take 3 classes. The following is preferable and will also lead to easier code:

| user  | class     |
|-------|-----------|
| user1 | 4/10/2017 |       
| user2 | 4/10/2017 | 
| user2 | 5/10/2017 |
|       |           | 

Also, do without the count and use --sessions[i].seats; as sessions[i].seats == count

1 Comment

Yeah, this is a rough first pass. Right now, a form Object is sent as a row in the spreadsheet. I need to add a loop to add them to the sheet.

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.