Why is this forEach loop returning 'null' for all elements but the last?

I am using Goolge App Scripts so that a Google Form can automatically create an event series that repeats weekly on Google Calendar. Right now, the form asks the user to select all weekdays that the event takes place in and places them into an array.

To create the event, I need to turn each element of the array into a CalendarApp.Weekday object. Here is what I have:

event = ['MONDAY', 'TUESDAY', 'THURSDAY', 'SATURDAY']; //Placed here as an example; this is
                                                         defined by the user.
event1 = []; // New array to hold CalendarApp.Weekday objects.

for (i = 0; i < event.length-1; i++); {
  event1[i] = `CalendarApp.Weekday.${event[i]}`
}//Expected output: [CalendarApp.Weekday.MONDAY, CalendarApp.Weekday.TUESDAY,
                     CalendarApp.Weekday.THURSDAY, CalendarApp.Weekday.SATURDAY]. 

The code returns: [null, null, null, CalendarApp.Weekday.SATURDAY].

For any combination of days that I try, everything returns "null" except for the last item, which returns the intended output. Any idea why, and how to fix it?

Thank you very much.

>Solution :

First remove the semi-colon after the for loop

for (i = 0; i < event.length; i++) {
  event1[i] = `CalendarApp.Weekday.${event[i]}`

and remove the -1 after event.length inside the loop.
The above code I have tweaked and should work fine, try copy pasting that.

Leave a Reply