// Numbas version: exam_results_page_options {"name": "Define a function in the preamble to reuse elsewhere", "extensions": [], "custom_part_types": [], "resources": [], "navigation": {"allowregen": true, "showfrontpage": false, "preventleave": false, "typeendtoleave": false}, "question_groups": [{"pickingStrategy": "all-ordered", "questions": [{"progress": "in-progress", "statement": "", "variables": {}, "name": "Define a function in the preamble to reuse elsewhere", "metadata": {"notes": "", "licence": "Creative Commons Attribution 4.0 International", "description": ""}, "type": "question", "parts": [{"answer": "x", "checkingaccuracy": 0.001, "scripts": {"mark": {"order": "instead", "script": "// this puts everything in the question.functions object into the current scope, so you can refer to it by its short name\nwith(question.functions) {\n \n // call the function defined in the preamble\n addNumbers(5,6);\n \n}"}}, "vsetrangepoints": 5, "checkingtype": "absdiff", "showCorrectAnswer": true, "type": "jme", "vsetrange": [0, 1], "checkvariablenames": false, "marks": 1, "expectedvariablenames": [], "showpreview": true}], "showQuestionGroupNames": false, "functions": {"a": {"type": "number", "definition": "return 1", "language": "javascript", "parameters": []}}, "tags": [], "variable_groups": [], "rulesets": {}, "preamble": {"css": "", "js": "// set up a dictionary called 'question.functions'\nquestion.functions = {};\n\n// define a function, and attach it to the functions object\n// the function is also assigned to the name 'addNumbers' in the local scope, so you can use it without typing `question.functions.` in front\nvar addNumbers = question.functions.addNumbers = function(a,b) {\n alert(a+' + '+b+' is '+(a+b));\n}\n\n// call the function as addNumbers here:\naddNumbers(1,2);\n\n// look at the part's marking script to see how the function's used there"}, "advice": "", "question_groups": [{"name": "", "questions": [], "pickQuestions": 0, "pickingStrategy": "all-ordered"}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}]}], "contributors": [{"name": "Christian Lawson-Perfect", "profile_url": "https://numbas.mathcentre.ac.uk/accounts/profile/7/"}]}