Development log – July 2014

Development on Numbas continues apace, so I thought it’d be a good idea to start writing about changes in more detail here so they don’t slip by unnoticed.

New JME functions

I’ve added the following new JME functions:

  • factorise(n)┬áreturns a list of the powers in the prime-power decomposition of $n$ (the simplest way I could think of to represent a factorisation as a list)
  • distinct(list)┬áreturns a copy of the input list, with duplicates removed.
  • pluralise(n,singular,plural)┬áreturns singular if $n$ is 1, and plural otherwise.
  • lcm now takes any number of arguments, and returns the lowest common multiple of all of them.

Pattern matching

I’ve vastly extended the pattern-matching algorithm (previously only used tom implement simple rewriting rules in the simplification algorithm) so it’s much more versatile. It now takes commutativity of operations into account, and has regular expression-like operators to do things like match any one of several patterns, match the negation of a pattern, and so on. There’s some very light documentation in the editor docs, and I also wrote a less coherent post on my personal blog about the thinking behind it.


I’ve brought the editor and Javascript API documentation up to date with all the recent changes. I’ve also added some information on writing extensions for Numbas.

Custom scripts

Each part now has a “when the part is created” script, which you can use to do some setup before the student sees the question. You could already do this in the question preamble, but you had to know the position of the part. This allows you to attach initialisation code to the part which doesn’t break when you add, move, or remove parts.

The preamble, and part scripts, now catch errors and display them, instead of just failing silently. This should make debugging a lot easier!

custom script error

Part scripts can access unwrapped, JavaScript-friendly versions of the question’s variables through the variables dictionary. The objects in question.scope.variables are wrapped up in a form for use by the JME system, which meant you had to do some work to unwrap them to use in JavaScript. The objects in the variables dictionary are unwrapped for you, so you can use them without having to write any boilerplate code at the start of your script.

There are also new variables called part and question, which point to the current part and its parent question. The magic JavaScript this variable already pointed to the current part object, but we felt it was better to use a clearer name.

In the question preamble, I’ve tidied up the methods to run code at different stages during initialisation. Now there are new methods question.onHTMLAttached and question.onVariablesGenerated, which each take a callback function to run once their respective tasks have finished.

Other changes

  • You can produce a normal dollar sign in text, without going into maths mode, by typing $. Thanks to Joshua Beals for pointing out this was missing.
  • I’ve finally fixed the bug which caused the “Saving…” message to get stuck when there’s an error in a question’s variable definitions.