Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading