not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1013 B

  1. var fs = require('fs');
  2. const WORD_FILE_PATH = '../words/words.txt';
  3. //loads words from word file
  4. var words = [];
  5. var data = fs.readFileSync(WORD_FILE_PATH, 'utf8');
  6. var lines = data.split('\n');
  7. lines.forEach(function(element){
  8. words.push(element);
  9. });
  10. module.exports=
  11. {
  12. /**
  13. * returns a specific amount of words -- unique
  14. * @param num the number of words
  15. * @returns {Array} the random, unique words
  16. */
  17. getRandomWords : function(num)
  18. {
  19. var rwords = [];
  20. for(var i = 0; i < num; ++i){
  21. var randindex = Math.round((Math.random() * (words.length - 1)));
  22. var newword = words[randindex];
  23. var uniq = true;
  24. rwords.forEach(function(element){
  25. if(newword === element){
  26. --i;
  27. uniq = false;
  28. }
  29. });
  30. if(uniq)rwords.push(newword);
  31. }
  32. return rwords;
  33. }
  34. };