-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
66 lines (57 loc) · 1.28 KB
/
index.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
// Requirements:
// A student management app that is able to:
// - Show current student list
// - Add new students
var readlineSync = require('readline-sync');
var fs = require('fs');
var students = [];
function loadData() {
var fileContent = fs.readFileSync('./data.json');
students = JSON.parse(fileContent);
}
function showMenu() {
console.log('1. Show all students');
console.log('2. Create a new student');
console.log('3. Save & Exit');
var option = readlineSync.question('> ');
switch (option) {
case '1':
showStudents();
showMenu();
break;
case '2':
showCreateStudent();
showMenu();
break;
case '3':
saveAndExit();
break;
default:
console.log('Wrong option');
showMenu();
break;
}
}
function showStudents() {
for (var student of students) {
console.log(student.name, student.age);
}
}
function showCreateStudent() {
var name = readlineSync.question('Name: ');
var age = readlineSync.question('Age: ');
var student = {
name: name,
age: parseInt(age)
};
students.push(student);
}
function saveAndExit() {
var content = JSON.stringify(students);
fs.writeFileSync('./data.json', content, { encoding: 'utf8' });
}
function main() {
loadData();
showMenu();
}
main();