Is this javascript macro (for "random number" functionality) okay?

G’day,

I need anybody who knows about javascript macros to lay their eyeballs on the code below.

  • As much as I dislike javascript, sometimes there just doesn’t seem to be any choice but to go there. The javascript is easy: copy paste from wherever whatever is needed.
  • Having a tendency to avoid javascript as if it was like the plague, I don’t particularly want to learn the mechanics of creating javascript macros in TiddlyWiki.

For the code below, I just copied now.js and setup the javascript I needed. Seems to work A-1, but I’d like to know if there is anything wise for me to change (i.e. “infrastructure” stuff re TiddlyWiki and javascript macros.) Let’s not think for a second that I have any clue what I’m doing…

If you have a moment to scrutinize the following, THANK-YOU !!!

/*\
title: Random Number
type: application/javascript
module-type: macro

Macro to return a random number

\*/
(function(){

/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";

/*
Information about this macro
*/

exports.name = "random";

exports.params = [
	{name: "maxvalue"}
];

/*
Run the macro
*/
exports.run = function(maxvalue) {
	return Math.floor(Math.random() * maxvalue)+1;
};

})();

Not sure. I think I’ve just used the maths random operator before.

=@floor((@random() * 10) + 1)

I think that gives you a whole number between one and ten.

Thanks, but it isn’t the javascript I need checking. As long as the javascript works, and it does, then I’m fine.

It is all of the other stuff wrapped around the javascript to have a TiddlyWiki javascript macro. I really have no clue if any of it is okay, and that is way too much for me to know intimately.

So Charlie, If I may paraphrase you

Does this macro comply with the way we write a javascript macro for use in tiddlywiki?

I think the answer may lay deep in the documentation about plugin development, but a simple answer will help Charlie (indirectly also me).

There is whole suit of simple macros that can be written that just make use of simple Javascript functions to obtain a result. Charlies being an example of one. If more of us knew how to and did so without breaking things, it would be helpful.

@Charlie_Veniot Perhaps you would get more attention for this in the developer Category or on Github discussions?.

1 Like

@Charlie_Veniot that looks fine. In general as long as your macro is only returning text and not doing anything else you should be OK.

Thanks @saqimtiaz. I was dazzled, deer in the headlights, by all the macro stuff wrapped around the javascript.

Much appreciated.