Repository navigation
Expand file tree
/
Copy pathFunctionScopeQnsClass.js
More file actions
112 lines (103 loc) · 4.01 KB
/
Copy pathFunctionScopeQnsClass.js
File metadata and controls
112 lines (103 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Contain the following type(s) of question:
- var a = 2;
function replace() {
a = 3;
}
replace();
console.log( a );
*/
class FunctionScopeTemplate {
constructor() {
this.question = this.generateQuestion(/*difficulty*/);
this.answer = this.generateAnswer(this.question[1]);
this.category = ['function'];
}
/*
Return 1 random alphabets
*/
generateAlphabet() {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
return alphabet[Math.floor(Math.random() * alphabet.length)];
}
/*
Return a random number from 1 to 5
*/
generateRnd5() {
return Math.floor(Math.random() * 5) + 1;
}
/*
Return a random name for the function
*/
generateFunctionName() {
var nameArr = ['change', 'replace', 'update']
return nameArr[Math.floor(Math.random() * 3)];
}
/*
Return an array consisting of:
[0] - function question
[1] - function question with variable for evaluation
*/
generateQuestion(/*difficulty*/) {
var alpha = this.generateAlphabet();
var paraName = this.generateAlphabet();
var fnName = this.generateFunctionName();
// Determine if there's parameter
var paraProb = Math.floor(Math.random() * 10);
var query = `\n\tvar ${alpha} = ${this.generateRnd5()};`;
var rnd = Math.floor(Math.random() * 10);
if (rnd % 3 == 1) { // 1,4,7
// Function involves parameter
var tempParaName;
if (paraProb % 3 == 0) {
// Parameter uses same name as variable
tempParaName = alpha;
} else {
// Parameter uses different name as variable
tempParaName = paraName;
}
query += `\n\tfunction ${fnName}( ${tempParaName} ) {`;
// Determine if there's shorthand operation within function
var shorthandProb = Math.floor(Math.random() * 10);
if (shorthandProb > 3) {
var rndOps = Math.floor(Math.random() * 10);
var operation = (rndOps < 4) ? '+=' : (rndOps < 7) ? '-=' : '*=';
query += `\n\t ${tempParaName} ${operation} ${this.generateRnd5() + 3};`;
} else {
query += `\n\t ${tempParaName} = ${this.generateRnd5() + 3};`;
}
query += `\n\t}\n\t${fnName}( ${alpha} );`;
} else {
// Function doesn't involve parameter
query += `\n\tfunction ${fnName}( ) {`;
// Determine if 'var' placed in front of variable in function
var innerVarProb = Math.floor(Math.random() * 10);
if (innerVarProb % 3 == 0) {
// keyword 'var' included in variable in function
query += `\n\t var ${alpha} = ${this.generateRnd5() + 3};`;
} else {
// keyword 'var' NOT included in variable in function
// Determine if there's shorthand operation within function
var shorthandProb = Math.floor(Math.random() * 10);
if (shorthandProb > 3) {
var rndOps = Math.floor(Math.random() * 10);
var operation = (rndOps < 4) ? '+=' : (rndOps < 7) ? '-=' : '*=';
query += `\n\t ${alpha} ${operation} ${this.generateRnd5() + 3};`;
} else {
query += `\n\t ${alpha} = ${this.generateRnd5() + 3};`;
}
}
query += `\n\t}\n\t${fnName}( );`;
}
var query2 = query + `\n${alpha}`; // NOTE: DO NOT INCLUDE C.L into EVAL fn
query += `\n\tconsole.log( ${alpha} );`;
return [query, query2];
}
/*
Return the evaulation of the question
*/
generateAnswer(question) {
return eval(question);
}
}
module.exports = FunctionScopeTemplate;