From 0e070cd46404247e691b0984848ddffa6576a431 Mon Sep 17 00:00:00 2001 From: Kellyn Date: Thu, 17 Jan 2019 20:16:18 -0600 Subject: [PATCH 1/4] 'index --- index.js | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 index.js diff --git a/index.js b/index.js new file mode 100644 index 0000000..77cd36a --- /dev/null +++ b/index.js @@ -0,0 +1,7 @@ + +const readline = require('readline'); + +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); \ No newline at end of file From 510cd00761fee51091834ed53ebae2c53317a9ce Mon Sep 17 00:00:00 2001 From: Kellyn Date: Sun, 20 Jan 2019 10:56:50 -0600 Subject: [PATCH 2/4] finish functionality --- index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 77cd36a..0507799 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,82 @@ const readline = require('readline'); +const fs = require('fs'); +const path = './package.json' + const rl = readline.createInterface({ input: process.stdin, output: process.stdout -}); \ No newline at end of file +}); + +const checkForNoJson = () => {; + return !fs.existsSync(path); +} +// const findJson = require('./package.json'); + +const checkForInit = (init) => { + return init === 'init'; +} + +const askQuestionsGetAnswers = () => { + + // objects to ask questions and store user replies + const questions = { + question1: 'Name ?', + question2: 'Gender ?', + question3: 'Religous affiliation ?', + question4: 'Burdesome Secret ?', + question5: 'Cartoon Character lookalike ?' + } + + // object to store user answers for JSON File + const answers = { + answer1: null, + answer2: null, + answer3: null, + answer4: null, + answer5: null, + } + + // variables declared for recursive function to ask question using rl.question and store the answer + let i = 0; + const questionArr = Object.values(questions); + const answersArr = Object.keys(answers); + + // recursive function + const typeQuestion = (questionArr, answersArr, i) => { + rl.question(questionArr[i], (answer) => { + answers[answersArr[i]] = answer; + if (i >= '4') { + writeJSONfile(answers); + } else { + i++; + typeQuestion(questionArr, answersArr, i); + } + }) + } + + typeQuestion(questionArr,answersArr, i); +} + +// Creates JSONfile based upon users answers to questions. Its called within the recursive function typeQuestion +const writeJSONfile = (answers) => { + const answersToJSON = JSON.stringify(answers); + fs.writeFileSync(path,answersToJSON,(err) => { + if (err) throw err; + }) + rl.close(); + } + + +rl.question('What do you want ', (init) => { + if(checkForInit(init)) { + if(checkForNoJson()) { + askQuestionsGetAnswers(); + } else { + rl.close(); + } + } else { + rl.close(); + } + }); \ No newline at end of file From 85c43cc1d2812333a7861fc257cbe976799b1531 Mon Sep 17 00:00:00 2001 From: Kellyn Date: Sun, 20 Jan 2019 12:15:36 -0600 Subject: [PATCH 3/4] updates --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 0507799..2cff2b0 100644 --- a/index.js +++ b/index.js @@ -62,10 +62,11 @@ const askQuestionsGetAnswers = () => { // Creates JSONfile based upon users answers to questions. Its called within the recursive function typeQuestion const writeJSONfile = (answers) => { const answersToJSON = JSON.stringify(answers); - fs.writeFileSync(path,answersToJSON,(err) => { + fs.writeFile(path,answersToJSON, (err) => { if (err) throw err; + console.log('Saved information'); + rl.close(); }) - rl.close(); } From 3d6bd8a28bd99ee80bf778834965ebceeacc38a0 Mon Sep 17 00:00:00 2001 From: Kellyn Date: Mon, 21 Jan 2019 19:15:05 -0600 Subject: [PATCH 4/4] added comments --- index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 2cff2b0..0c12341 100644 --- a/index.js +++ b/index.js @@ -9,24 +9,27 @@ const rl = readline.createInterface({ output: process.stdout }); +// function that checks if JSON file 'package.json' exists const checkForNoJson = () => {; return !fs.existsSync(path); } -// const findJson = require('./package.json'); + +// function to check if users first input is init const checkForInit = (init) => { return init === 'init'; } +// function that stores two objects(questions, user answers), and includes a recursive function that asks the user 5 questions const askQuestionsGetAnswers = () => { - // objects to ask questions and store user replies + // object to ask questions const questions = { - question1: 'Name ?', - question2: 'Gender ?', - question3: 'Religous affiliation ?', - question4: 'Burdesome Secret ?', - question5: 'Cartoon Character lookalike ?' + question1: 'Name?: ', + question2: 'Gender?: ', + question3: 'Religous affiliation?: ', + question4: 'Burdesome Secret?: ', + question5: 'Cartoon Character lookalike?: ' } // object to store user answers for JSON File @@ -70,7 +73,7 @@ const writeJSONfile = (answers) => { } -rl.question('What do you want ', (init) => { +rl.question('', (init) => { if(checkForInit(init)) { if(checkForNoJson()) { askQuestionsGetAnswers();