-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
197 lines (184 loc) · 5.02 KB
/
app.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
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
var BasicCard = require("./BasicCard.js");
var ClozeCard = require("./ClozeCard.js");
var inquirer = require("inquirer");
var fs = require("fs");
//Greeting
console.log(
"**************** FlashCard Generator! ***********************"
);
console.log(
"* You can make two types of cards, Basic and Cloze. A Basic card *"
);
console.log(
"* will have a question on the front and the answer on the back. A *"
);
console.log(
"* Cloze card will have a sentence or question that has had some *"
);
console.log(
"* of its text removed. Once you make your cards, test your *"
);
console.log(
"* knowledge by quizzing yourself! *"
);
console.log(
"**************** Have fun! ***********************"
);
console.log("");
console.log("");
console.log("");
function app() {
inquirer
.prompt([
{
name: "route",
message: "What would you like to do?",
type: "list",
choices: [
"Make a Basic Card",
"Make a Cloze Card",
"Show me existing cards",
"Quiz me!",
"Quit"
]
},
{
name: "basicCardFront",
message: "What would you like to be on the front of the card?",
when: function(answers) {
return answers.route === "Make a Basic Card";
}
},
{
name: "basicCardBack",
message: "What would you like to be on the back of the card?",
when: function(answers) {
return answers.basicCardFront;
}
},
{
name: "clozeCardFull",
message: "What is the full question?",
when: function(answers) {
return answers.route === "Make a Cloze Card";
}
},
{
name: "clozeCardhiddens",
message: "What should I hide from the question?",
when: function(answers) {
return answers.clozeCardFull;
}
}
])
.then(function(answers) {
if (answers.route === "Make a Basic Card") {
var newBasicCard = new BasicCard(
answers.basicCardFront,
answers.basicCardBack
);
newBasicCard.createCard();
setTimeout(app, 1000);
} else if (answers.route === "Make a Cloze Card") {
if (answers.clozeCardFull.includes(answers.clozeCardhiddens)) {
var newClozeCard = new ClozeCard(
answers.clozeCardFull,
answers.clozeCardhiddens
);
newClozeCard.createCard();
setTimeout(app, 1000);
} else {
console.log(
"Error: Sorry your hidden word was not in the question, try again."
);
setTimeout(app, 1000);
}
} else if (answers.route === "Show me existing cards") {
fs.readFile("log.txt", "utf8", function(err, data) {
//turn the log file into an array
var questionsArray = data.split(";");
questionsArray.splice(questionsArray.length - 1);
console.log(questionsArray);
setTimeout(app, 1000);
});
} else if (answers.route === "Quiz me!") {
fs.readFile("log.txt", "utf8", function(err, data) {
var questionsArray = data.split(";");
questionsArray.splice(questionsArray.length - 1);
var index = 0;
playFlashCards(questionsArray, index);
});
} else if (answers.route === "Quit") {
inquirer
.prompt([
{
name: "quit",
message: "Are you sure?",
type: "confirm"
}
])
.then(function(answer) {
if (answer.quit) {
console.log("Come back soon!");
} else {
app();
}
});
}
});
}
function playFlashCards(array, index) {
var questions = array[index];
var parsedQuestion = JSON.parse(questions);
var question;
var correctAnswer;
if (parsedQuestion.type === "basic") {
question = parsedQuestion.front;
correctAnswer = parsedQuestion.back;
} else if (parsedQuestion.type === "cloze") {
question = parsedQuestion.fullText;
correctAnswer = parsedQuestion.cloze;
}
inquirer
.prompt([
{
name: "quiz",
message: question
}
])
.then(function(answer) {
if (answer.quiz === correctAnswer) {
console.log("correct");
if (array.length - 1 > index) {
playFlashCards(array, index + 1);
} else {
playAgain();
}
} else {
console.log("Incorrect");
if (array.length - 1 > index) {
playFlashCards(array, index + 1);
} else {
playAgain();
}
}
});
}
function playAgain() {
inquirer
.prompt([
{
name: "playAgain",
message: "Done with all the questions, Go to the Main Menu?",
type: "confirm"
}
])
.then(function(answer) {
if (answer.playAgain) {
app();
} else {
console.log("Come back soon!");
}
});
}
app();