Repository navigation
Expand file tree
/
Copy pathPostPreIncreDecreQnsClass.js
More file actions
86 lines (80 loc) · 2.57 KB
/
Copy pathPostPreIncreDecreQnsClass.js
File metadata and controls
86 lines (80 loc) · 2.57 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
/*
Contain the following type(s) of question:
- var i = 2;
i = --i + --i + --i;
console.log( i );
*/
class PostPreIncreDecreOperatorTemplate {
constructor() {
this.question = this.generateQuestion();
this.answer = this.generateAnswer(this.question[1]);
this.category = ['post-increment', 'pre-increment', 'post-decrement', 'pre-decrement'];
}
/*
Return a random alphabet
*/
generateAlphabet() {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
return alphabet[Math.floor(Math.random() * alphabet.length)];
}
/*
Return a random number from 2 to 5
*/
generateRnd5() {
return Math.floor(Math.random() * 4) + 2;
}
/*
Return 1 post/pre-increment/decrement (PPID) value
*/
generatePPINValue(alpha) {
var rnd = Math.floor(Math.random() * 10);
if (rnd <= 2.5) {
return `++${alpha}`;
} else if (rnd > 2.5 && rnd <= 5) {
return `${alpha}++`;
} else if (rnd > 5 && rnd <= 7.5) {
return `--${alpha}`;
}
return `${alpha}--`;
}
/*
Return an array of 'x' amount of post/pre-increment/decrement values
*/
generateNoOfValues(alpha) {
var arrOptions = [], x;
x = 2;
for (var i = 0; i < x; i++) {
arrOptions.push(this.generatePPINValue(alpha));
}
return arrOptions;
}
/*
Return an array consisting of:
[0] - string concatenated PPID question
[1] - string concatenated PPID question with variable for evaluation
*/
generateQuestion() {
var alpha = this.generateAlphabet();
var values = this.generateNoOfValues(alpha);
var query = `\tvar ${alpha} = ${this.generateRnd5()};\n\t${alpha} = `;
var question = values[0];
for (var i = 1; i < values.length; i++) {
if (Math.floor(Math.random() * 10) % 2 == 0) {
question += ' + ' + values[i];
} else {
question += ' - ' + values[i];
}
}
var query2 = query + question + `; \n${alpha}`; // NOTE: DO NOT INCLUDE C.L into EVAL fn
query += question + `;\n\tconsole.log( ${alpha} );`;
//var query2 = query + `\n\t${alpha}`;
return [query, query2];
}
/*
Return the evaulation of the question
*/
generateAnswer(question) {
return eval(question);
}
}
module.exports = PostPreIncreDecreOperatorTemplate;