JavaScript - Append Text to a Draft

Still working to figure out JavaScript here - trying to set up a front-end for sending text to Fantastical and append text to a draft that contains the calendar type. In this simple example below, every time I run the script - and no matter whether I press Work or Home button, the text from the first if statement is added (" /Calendar"). It seems like I am passing the button pressed to the variable in the if statements. Is there something else I am missing here?

Thanks for any pointers!

/*
  Ask for a calendar type, add tag to draft based on calendar type
*/

// setup calendar categories
const categories = ["Work", "Home"];

// prompt to select a category
var p = Prompt.create();
p.title = "Select calendar";
for (var cat of categories) {
        p.addButton(cat);
}

if (p.show()) { // user made a selection
        var calendar = p.buttonPressed;

var d = draft;

   if (calendar = "Work") {
       d.content = d.content + " /Calendar "
            d.update();
   } else if (calendar = "Home") {
       d.content = d.content + " /Outlook "
            d.update();
   } else {
   }

} else {
        context.cancel();
}

You need double equal for comparisons inside the if:

calendar == “xxxx”

1 Like

Yep - worked like a charm! Thanks so much.

and triple === would be even better

( identity is safer and faster than equality )

(see under the ‘evil twins’ == and != )

https://books.google.co.uk/books?id=PXa2bby0oQ0C&pg=PA109&lpg=PA109&dq=crockford+evil+twins&source=bl&ots=HKnop3x1kG&sig=PnBnFzMtuu6pv1wAGYkgguZyiEA&hl=en&sa=X&ved=2ahUKEwjw_a3qhabbAhWJAMAKHeJgB6cQ6AEwAHoECAYQAQ#v=onepage&q=crockford%20evil%20twins&f=false

Triple it is! Thanks for all the pointers in this thread. Back to my JavaScript book…

1 Like