Simple NodeJS Example

Simple NodeJS Example

von Mark Tyers -
Anzahl Antworten: 1

Hi

I am struggling to build a simple NodeJS question: student to create a function to add 2 numbers and return the sum.

There don't seem to be any examples using this language even though CodeRunner supports NodeJS

Has anyone got a simple working example please?

Als Antwort auf Mark Tyers

Re: Simple NodeJS Example

von Richard Lobb -

Sorry Mark, it looks like nodejs support has fallen by the wayside a bit. NodeJS now enforces strict mode and the current CodeRunner nodejs prototype breaks. Fortunately, it's easy to fix.

If you have administrator rights on the Moodle system, navigate in the question bank to the category Top for System, where you should find a question BUILT_IN_PROTOTYPE_nodejs. Edit that question type (ignore the "Proceed at your own risk" warning) and replace the line in the template

SEPARATOR = "#<ab@17943918#@>#"

with

var SEPARATOR = "#<ab@17943918#@>#";

You should then be able to create a nodejs-2 CodeRunner question that says "Write a function add(n1, n2) that returns the sum of n1 and n2" with test cases like

console.log(add(23, 11));

and expected output

34

In case you don't have admin rights, I attach an xml file that you can import. It defines the fixed nodejs prototype (defining a question type nodejs rather than nodejs-2) and the add() example question. 

Sorry about the hassles. I'll fix the prototype in the next release and add the sample question.

Richard