Skip to content

Instantly share code, notes, and snippets.

@3mcd
Last active December 17, 2015 21:56
Show Gist options
  • Save 3mcd/c98eacc216d0cbfc23ca to your computer and use it in GitHub Desktop.
Save 3mcd/c98eacc216d0cbfc23ca to your computer and use it in GitHub Desktop.
var assert; // TODO: write some assert lib/function
function success() {
// TODO: make some call to store student answer
}
function failure() {
// TODO: make some call to store student answer, update notebook with hints, etc
}
var notebook = Tonic.createNotebook({
element: document.getElementById("my-element"),
source: ({x, y, z} = context) => `
// Using the mean() function above, find the mean of `x`, `y`, and `z`.
var x = ${x}, y = ${y}, z = ${z};
`,
// Contains default values/global variables
context: {
x: 5,
y: 10,
z: 20,
mean: (...args) =>
args.reduce((a, x) => a + x, 0) / args.length
},
onResponse: function ({ body, meta } = res) {
/**
* `body` is simply the response from Tonic. For example,
* if the cell was executed with the value `10`, the body
* would be `10` or similar.
*
* `meta` could potentially contain information from static
* code analysis; like what functions were called, the value
* of x, y, z etc
*/
const { mean } = this.context;
const { x, y, z } = meta.context;
assert(body == mean(x, y, z), success, failure);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment