-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathword.js
71 lines (64 loc) · 1.88 KB
/
word.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
/*:::::::::::: REQUIRE :::::::::*/
const randomWords = require('random-words');
const fs = require('fs');
const jsonfile = require('jsonfile');
/*:::::::::::: GLOBAL VARIABLES :::::::::*/
var nwWord = '';
/*:::::::::::: FUNCTIONS :::::::::*/
function genJFile(data) {
obj = JSON.parse(data);
var file = 'logs/data.json';
jsonfile.writeFile(file, obj, {
flag: 'a'
}, function (err) {
if (err) {
console.log(err);
return;
}
// console.log("data.json was updated!");
});
}
// this function randomly generates a word
function genWord() {
// uses NPM module to gen word
var word = randomWords();
// this loops through word and pushes blanks to blanks array and letter to letter array
var letters = [];
var blanks = [];
var count = word.length;
for (var w = 0; w < word.length; w++) {
letters.push(word[w]);
blanks.push("_");
}
//console.log(letters);
//console.log(blanks);
// sends values to generate the object
genOBJ(word, letters, blanks, count);
}
// this function generates the word object
function genOBJ(word, letters, blanks, count) {
// constructor function the creates word obj
function Word(count, word, letters, blanks) {
this.count = count;
this.word = word;
this.letters = JSON.stringify(letters);
this.blanks = JSON.stringify(blanks);
this.date = Date.now();
};
Word.prototype.printWord = function () {
var data = "Count: " + this.count + "\nWord: " + this.word + " \nLetters: " + this.letters + " \nBlanks: " + this.blanks + "\nDate: " + this.date;
};
// generates the new word object
nwWord = new Word(count, word, letters, blanks);
// prints object to console
nwWord.printWord();
//console.log(nwWord);
// appends the word object to logs/data.json
genJFile(JSON.stringify(nwWord));
// exports the word object
}
// run genWord function
genWord();
module.exports = {
word: nwWord,
}