-
Notifications
You must be signed in to change notification settings - Fork 1
ParametricProblems
To make parameteric problems, the problem author supplies a file param.js (convention over configuration).
The file can contain arbitrary JavaScript instructions. It populates an object whose name is param.
For example:
#!javacript
var param = function() {
angle;
direction;
directionDescr;
}
function randInt(n) { return Math.floor(n * Math.random()) }
param.angle = 90 * (1 + randInt(3)) // 90, 80, 270
param.direction = -1 + 2 * randInt(2) // -1, 1
param.directionDescr = param.direction == -1 ? "clockwise" : "counterclockwise"
//testing purpose
console.log("angel = " + param.angle);
console.log("direction = " + param.direction);
console.log("desc = " + param.directionDescr);
Use the Java scripting API (Core Java Vol 2 Ch 10) to process the file, providing an empty param map in the binding. Upon completion, the param map contains all variables that the problem designer wanted to specify.
Now search the problem description for any instance of {variable}
, where variable
is any valid JavaScript variable, such as {angle}
or {directionDescr}
. Substitute it with the value, and present the problem.html to the student. In that HTML, make hidden fields for each of the variables as well. (Do something to separate them from the other variables in the form, e.g. prefix with param_
.)
When the student submits the form, make the same substitutions on all the source files in the problem instance. Then run CodeCheck in the usual way.