How:Write scripts in JavaScript for Memento: Difference between revisions

Jump to navigation Jump to search
m
no edit summary
mNo edit summary
mNo edit summary
(One intermediate revision by the same user not shown)
Line 30: Line 30:
:; 2 * currentWeight: If currentWeight is 23 lbs, then 46 lbs
:; 2 * currentWeight: If currentWeight is 23 lbs, then 46 lbs
:; (hours * 60) + minutes:If hours is 5 and minutes is 10, then 310
:; (hours * 60) + minutes:If hours is 5 and minutes is 10, then 310
==== Conditional execution in JavaScript ====
Deciding to execute one set of statements or another or possibly yet another
based on some condition is referred to as ''conditional execution'' and forms of it
are known as ''alternation''. What one might want to do is:
If something is true, then do these things; otherwise, do those things.
This is referred to as alternation.
if (field("products") == "Orange")
    "Fruit"
else if (field("products") == "Broccoli")
    "Vegetable"
else
    "Empty"
_____
If you read other people's scripts, you might see ...
_____
if (field("products") == "Orange")
    "Fruit";
else if (field("products") == "Broccoli")
    "Vegetable";
else
    "Empty";
_____
That's because Memento tries to keep things simple for us, and semicolons look weird too, and for just an expression, it's not really needed, but when things get more complex, it turns out you need to terminate every statement with a semicolon. That's why professional JavaScript programmers do that. Another similar thing you might see is ...
if (field("products") == "Orange") {
    "Fruit";
}
else if (field("products") == "Broccoli") {
    "Vegetable";
}
else {
    "Empty";
}
The reason for that is that sometimes, when what you're needing to do is a little more complex, you might need to do several things if the produce is an orange, for example, and the way you do that is to use braces at the start & at the end of a block of statements. That's why we call those things blocks. In your case now it's overkill & you don't need it, but it still works if you do, as above.
If you have just a couple more of each type of produce, you can use || which means or. That's or, & for and, you say &&.  If you do a lot of this stuff, it starts to get hard to match up an end parenthesis with its corresponding start parenthesis. In Calculation fields, you have that problem in spades, even with simple stuff (another reason I don't use calc), but it happens in JavaScript too, for somewhat more complex stuff. For example ...
_____
if ((field("products") == "Orange") || (field("products") == "Lemon"))
    "Fruit";
else if ((field("products") == "Broccoli") || (field("products") == "Spinach"))
    "Vegetable";
else
    "Empty";
_____
Repeat the else if line pair as needed.
If you end up doing a lot of them, there's another way to do it that would be more compact & will help with the parentheses ...
_____
// Comment here, to the end of the line, ignored by JavaScript
var produce = field("GrocerySection");  // Replace with your fieldname
switch (expr) {
  case "Orange":
  case "Lemon";
  case "Lime":
  case "Tangerine":
      "Fruit";
      break;
  case "Broccoli":
  case "Brussel sprouts";
  case "Turnips":
  case "Spinach":
      "Vegetable";
      break;
  default:
      "Empty";
}
_____
So, we have one or more cases, then the result, then a command to end the list of cases. I've always thought this (the break) is stupid, but it works, and that's the way they did it, so that's how we have to do it. Then another one or more cases then the result and the break, and on and on. Finally, if it matches none of the kinds of produce, then you can supply the default result by using default in place of case. There can be only one default.
So ...
This message is already quite long, so I won't take you to the next stage of complexity yet, but we use the next form in action scripts a lot & in trigger scripts sometimes too, so if you're interested, give me another shout, and I'll pick up with the next lesson.


==== Sample complex expression script ====
==== Sample complex expression script ====
Line 80: Line 162:
function formatMoney(number, decPlaces, decSep, thouSep) {
function formatMoney(number, decPlaces, decSep, thouSep) {
     decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
     decPlaces = isNaN(decPlaces = Math.abs(decPlaces)) ? 2 : decPlaces,
     decSep = typeof decSep === "undefined" ? "." : decSep;
     decSep = typeof decSep === "undefined" ? "." : decSep,
     thouSep = typeof thouSep === "undefined" ? "," : thouSep;
     thouSep = typeof thouSep === "undefined" ? "," : thouSep;
     var sign = number < 0 ? "-" : "";
     var sign = number < 0 ? "-" : "";

Navigation menu