-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRepetitionDoWhileQnsClass.js
68 lines (59 loc) · 1.98 KB
/
RepetitionDoWhileQnsClass.js
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
/*
Contain the following type(s) of question:
- var count = 1;
do {
count++;
} while(count < 5)
console.log( count );
*/
class DoWhileRepetitionTemp {
constructor() {
this.question = this.generateQuestion();
this.answer = this.generateAnswer(this.question[1]);
this.category = ['do while loop'];
}
/*
Return a random alphabet
*/
generateAlphabet() {
const alphabet = "abcdefghijklmnopqrstuvwxyz";
return alphabet[Math.floor(Math.random() * alphabet.length)];
}
/*
Return a random number from 0 to 9
*/
generateRnd10() {
return Math.floor(Math.random() * 10);
}
/*
Return an array consisting of:
[0] - string concatenated do while loop question
[1] - string concatenated do while loop question with counter for evaluation
*/
generateQuestion() {
var alpha = this.generateAlphabet();
var small = Math.floor(Math.random() * 5);
var big = Math.floor(Math.random() * 10) + 15;
var query = `\n\tvar ${alpha} = `;
var query2 = `// do-while `;
if (this.generateRnd10() % 2 == 0) {
// Increment do-while Loop
var sign = (this.generateRnd10() % 2 == 0) ? '<' : '<=';
query += `${small};\n\tdo {\n\n\t ${alpha}++;\n\n\t} while( ${alpha} ${sign} ${big} );`;
} else {
// Decrement do-while Loop
var sign = (this.generateRnd10() % 2 == 0) ? '>' : '>=';
query += `${big};\n\tdo {\n\n\t ${alpha}--;\n\n\t} while( ${alpha} ${sign} ${small} );`;
}
query2 += query + `\n${alpha}`
query += `\n\tconsole.log( ${alpha} );`
return [query, query2];
}
/*
Return the evaulation of the question
*/
generateAnswer(question) {
return eval(question);
}
}
module.exports = DoWhileRepetitionTemp;