-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayBasicQnsClass.js
More file actions
203 lines (190 loc) · 5.69 KB
/
Copy pathArrayBasicQnsClass.js
File metadata and controls
203 lines (190 loc) · 5.69 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*
Contain the following type(s) of question:
- accessing 1D array
- accessing 2D array
- get length of 1D, 2D array
- .pop() function
- .push() function
- basic mathematical operations among values in array
*/
class ArrayBasicTemp {
constructor() {
this.question = this.generateQuestion();
this.answer = this.generateAnswer(this.question[1]);
this.category = ["array"];
}
/*
Return a random alphabet
*/
generateAlphabet() {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
return alphabet[Math.floor(Math.random() * alphabet.length)];
}
/*
Return a random number from x to 4 + x
*/
generateArrayLen(x) {
return Math.floor(Math.random() * 4) + x;
}
/*
Return an array consisting of:
[0] - string concatenated array of random size 2 to 4
[1] - string concatenated array's length
*/
generateInnerArray() {
var arrLen = Math.floor(Math.random() * 3) + 2;
var query = "[ ";
for (let i = 0; i < arrLen; i++) {
query += `${Math.floor(Math.random() * 10)}`;
if (i != arrLen - 1) {
query += ", ";
}
}
query += " ]";
return [query, arrLen];
}
/*
Return an 1D/ 2D array consisting of:
[0] - string concatenated .length question
[1] - string concatenated .length question with len for evaluation
*/
generateGetLengthQns() {
var arrLen = this.generateArrayLen(4);
var query = "\tvar arr = ";
if (Math.floor(Math.random() * 10) < 5) {
// generate 1D array
query += "[ ";
for (let i = 0; i < arrLen; i++) {
query += `${Math.floor(Math.random() * 10)}`;
if (i != arrLen - 1) {
query += ", ";
}
}
query += " ];";
} else {
// generate array use new keyword
if (Math.floor(Math.random() * 10) < 6) {
query += `new Array(${arrLen});`;
} else {
query += `new Array(`;
for (let i = 0; i < arrLen; i++) {
query += `${Math.floor(Math.random() * 10)}`;
if (i != arrLen - 1) {
query += ", ";
}
}
query += `);`;
}
}
query += "\n\tvar len = arr.length;\n";
var query2 = query + "// length \nlen";
query += "\tconsole.log( len );";
return [query, query2];
}
/*
Return an 1D/ 2D array consisting of:
[0] - string concatenated get value by index question
[1] - string concatenated get value by index question with variable for evaluation
*/
generateGetIndexQns() {
var arrLen = this.generateArrayLen(5);
var alpha = this.generateAlphabet();
var query = "\tvar arr = [ ";
var query2;
// generate 1D array
for (let i = 0; i < arrLen; i++) {
query += `${Math.floor(Math.random() * 10)}`;
if (i != arrLen - 1) {
query += ", ";
}
}
query += ` ];\n\tvar ${alpha} = arr[${Math.floor(
Math.random() * arrLen
)}];\n`;
query2 = query + `${alpha}`;
query += `\tconsole.log( ${alpha} );`;
return [query, query2];
}
/*
Return an 1D array consisting of:
[0] - string concatenated index operations question
[1] - string concatenated index operations question with variable for evaluation
*/
generateIndexOperationQns() {
var arrLen = this.generateArrayLen(6);
//var alpha = this.generateAlphabet();
var actualArray = [];
var query = "\tvar arr = [ ";
var query2;
// generate array
for (let i = 0; i < arrLen; i++) {
var rnd = Math.floor(Math.random() * 10);
query += `${rnd}`;
actualArray.push(rnd);
if (i != arrLen - 1) {
query += ", ";
}
}
// performing arithmetic operation
query += ` ];\n\tvar output = arr[${Math.floor(Math.random() * arrLen)}]`;
var noOfVar;
noOfVar = Math.floor(Math.random() * 10) % 3 == 0 ? 1 : 2;
for (var i = 0; i < noOfVar; i++) {
var rnd = Math.floor(Math.random() * 10);
query +=
rnd < 5
? ` - arr[${Math.floor(Math.random() * arrLen)}]`
: ` + arr[${Math.floor(Math.random() * arrLen)}]`;
}
query += `;\n`;
query2 = query + `output`;
query += `\tconsole.log( output );`;
return [query, query2];
}
/*
Return either .pop() or .push() question:
[0] - string concatenated format
[1] - string concatenated format for evaluation
*/
generateArrayMethodsQns(/*difficulty*/) {
var arrLen = this.generateArrayLen(4);
var alpha = this.generateAlphabet();
var query = "\tvar arr = ";
var query2;
// generate 1D array
query += "[ ";
for (let i = 0; i < arrLen; i++) {
query += `${Math.floor(Math.random() * 10)}`;
if (i != arrLen - 1) {
query += ", ";
}
}
query += " ];";
if (Math.floor(Math.random() * 10) < 5) {
// pop question
query += `\n\tvar ${alpha} = arr.pop();`;
query2 = query + `\n// pop `;
} else {
// push question
query += `\n\tvar ${alpha} = arr.push( ${Math.floor(
Math.random() * 10
)} );`;
query2 = query + `\n// push `;
}
}
// Generate random array questions
generateQuestion() {
let random = Math.floor(Math.random() * 4);
let arrayOfQuestions = [
this.generateGetLengthQns(),
this.generateGetIndexQns(),
this.generateIndexOperationQns(),
this.generateArrayMethodsQns(),
];
return arrayOfQuestions[random];
}
generateAnswer(question) {
return eval(question);
}
}
module.exports = ArrayBasicTemp;