Browse Source

implemented random word retrieval functionality

master
ritfsaecdaq 6 years ago
parent
commit
f046d03ca1
1 changed files with 26 additions and 15 deletions
  1. +26
    -15
      googletrendsgame/server/utils.js

+ 26
- 15
googletrendsgame/server/utils.js View File

@ -1,26 +1,37 @@
var words = [];
var fs = require('fs');
const WORD_FILE_PATH = '../word_selection/words.txt';
//loads words from word file
var words = [];
var data = fs.readFileSync(WORD_FILE_PATH, 'utf8');
var lines = data.split('\n');
lines.forEach(function(element){
words.push(element);
});
module.exports=
{
/**
* Returns a random word
* @returns {string}
*/
getRandomWord : function()
{
return '';
},
/**
* returns a specific amount of words -- unique
* @param num
* @returns {Array}
* @param num the number of words
* @returns {Array} the random, unique words
*/
getRandomWords : function(num)
{
return [];
var rwords = [];
for(var i = 0; i < num; ++i){
var randindex = Math.round((Math.random() * (words.length - 1)));
var newword = words[randindex];
var uniq = true;
rwords.forEach(function(element){
if(newword === element){
--i;
uniq = false;
}
});
if(uniq)rwords.push(newword);
}
return rwords;
}
};
};

Loading…
Cancel
Save