Javascript loop question

Hi there. I’m trying to create a JavaScript loop that creates a bunch of reminders with incrementing dates. I have stored all the various dates into individual variables named d1, d2, d3, etc. What I would like to do is create a loop where the reminder is created and assigned these variables. The code I’m working with is below.

My basic question is: how do I increment the date variables? I suspect if I write d+i that won’t quite be right. (I’m talking about the line near the bottom that reads task.dueDate = d1;

// Note repetition to Reminders

var link = app.getClipboard();
var d1 = Date.today().addHours(22);
var d2 = Date.today().add({ days: 1, hours: 22 });
var d3 = Date.today().add({ days: 4, hours: 22 });
var d4 = Date.today().add({ days: 6, hours: 22 });
var d5 = Date.today().add({ days: 8, hours: 22 });
var d6 = Date.today().add({ days: 11, hours: 22 });
var d7 = Date.today().add({ days: 14, hours: 22 });
var d8 = Date.today().add({ days: 22, hours: 22 });
var d9 = Date.today().add({ days: 30, hours: 22 });
var d10 = Date.today().add({ days: 60, hours: 22 });
var d11 = Date.today().add({ days: 90, hours: 22 });
var d12 = Date.today().add({ days: 120, hours: 22 });

// Set main variables

var list = ReminderList.findOrCreate("Routines");
var notetitle = "review note: " + draft.processTemplate("[[title]]"); 


// add todos to the project

For (var i = 1; i < 13; i++) {
	var task = 		list.createReminder();
	task.title = notetitle;
	task.dueDate = d1;
	task.notes = link;
	task.update();
}

To loop over them, you need the dates in a data structure that can be iterated, like an array. You can create a array of dates, then iterate over them in a for loop like:

let dates = [
	Date.today().addHours(22),
	Date.today().add({ days: 1, hours: 22 }),
	Date.today().add({ days: 4, hours: 22 }),
	Date.today().add({ days: 6, hours: 22 }),
	Date.today().add({ days: 8, hours: 22 }),
	Date.today().add({ days: 11, hours: 22 }),
	Date.today().add({ days: 14, hours: 22 }),
	Date.today().add({ days: 22, hours: 22 }),
	Date.today().add({ days: 30, hours: 22 }),
	Date.today().add({ days: 60, hours: 22 }),
	Date.today().add({ days: 90, hours: 22 }),
	Date.today().add({ days: 120, hours: 22 })
];

for (let d of dates) {
    //create the reminder
    // the variable `d` will contain a date each time
}

Aha. This is great. With the “for (let d of dates)” line, is “d” just any random variable you want to create here, or does the “d” have a special meaning?

Any valid name for a JavaScript variable will suffice.

For the curious reader later on, here’s the revised script. Thanks for the basic JavaScript coding tutorial @agiletortoise !

// Note repetition to Reminders

var link = app.getClipboard();

let dates = [
	Date.today().addHours(22),
	Date.today().add({ days: 1, hours: 22 }),
	Date.today().add({ days: 4, hours: 22 }),
	Date.today().add({ days: 6, hours: 22 }),
	Date.today().add({ days: 8, hours: 22 }),
	Date.today().add({ days: 11, hours: 22 }),
	Date.today().add({ days: 14, hours: 22 }),
	Date.today().add({ days: 22, hours: 22 }),
	Date.today().add({ days: 30, hours: 22 }),
	Date.today().add({ days: 60, hours: 22 }),
	Date.today().add({ days: 90, hours: 22 }),
	Date.today().add({ days: 120, hours: 22 })
];

// Set main variables

var list = ReminderList.findOrCreate("Routines");
var notetitle = "review note: " + draft.processTemplate("[[title]]"); 

// add todos to the project

for (let d of dates) {
	var task = list.createReminder();
	task.title = notetitle;
	task.dueDate = d;
	task.notes = link;
	task.update();
}

1 Like