-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom-student.while-loop.js
More file actions
83 lines (69 loc) · 2.03 KB
/
random-student.while-loop.js
File metadata and controls
83 lines (69 loc) · 2.03 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
const fs = require('fs');
const gradient = require('gradient-string');
const CFonts = require('cfonts');
const isResetEnabled = process.argv[2] === 'reset';
const logFile = __dirname + '/random-student.log';
let students = [];
function init() {
students = require('../students.js');
readHistoryAndCall();
}
function readHistoryAndCall() {
// Call history file.
// Read history log on each run.
fs.readFile(logFile, 'utf-8', (err, history) => {
if (err) throw err;
let student = call(history);
// console.log();
const clrs = [
"black",
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"white",
// "blackBright",
"redBright",
"greenBright",
"yellowBright",
"blueBright",
"magentaBright",
"cyanBright",
"whiteBright"
];
CFonts.say(student, {
colors: [clrs[Math.floor(Math.random() * clrs.length)]]
});
})
}
function getRandom() {
return students[Math.floor(Math.random() * students.length)];
}
function call(history) {
// Remove white-space then split lines into array.
const called = history.trim().split('\n');
// After all students have been called OR reset argument specified.
if (students.length <= called.length || isResetEnabled) {
called.length = 0;// remove all students from the called list.
fs.writeFile(logFile, '', (err) => {
if (err) throw err;
console.log(gradient.instagram('All students called! Clearing called list!'));
});
}
// Get random student.
let student = getRandom();
// Skip student if called already, while others haven't been called yet.
while (called.includes(student) && students.length !== called.length) {
console.log(gradient.mind(student + ' is included already!'))
// Pick a new random student.
student = getRandom();
}
fs.appendFile(logFile, '\n' + student, (err) => {
if (err) throw err;
});
return student;
}
//-------------------------------------------------
init();