-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.js
More file actions
35 lines (27 loc) · 928 Bytes
/
Copy pathapi.js
File metadata and controls
35 lines (27 loc) · 928 Bytes
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
var bodyParser = require('body-parser');
var fileName = './data/questions.json';
var data = require(fileName);
var fs = require('fs');
module.exports = {
getNextQuestion: getQuestion,
resetQuestion: resetQuestion,
processAnswer : function(body){
var nextQuestion = getQuestion();
return nextQuestion;
}
};
function getQuestion(){
var nextQuestion = data.questions[data.currentQuestion];
var totalQuestions = Object.keys(data.questions).length;
data.currentQuestion= data.currentQuestion + 1 == totalQuestions ? 0 : data.currentQuestion + 1;
fs.writeFile(fileName, JSON.stringify(data, null, 2), function (err) {
if (err) return console.log(err);
});
return nextQuestion;
}
function resetQuestion(){
data.currentQuestion = 0;
fs.writeFile(fileName, JSON.stringify(data, null, 2), function (err) {
if (err) return console.log(err);
});
}