'use strict';

let app = {};

(function (module){

  const words = {};
  
  words.all = [];


// this array will contain 3 arrays of data from API
words.genArray = []

let answersArray = ['sl=bird','rel_trg=tiny','rel_trg=pig']

  words.requestWords = function (callback){
    answersArray.forEach(function(value){
      $.get('/datamuse/api/' + value)
        .then ( function(data) {
        console.log("data:",data)
        words.genArray.push(data)
        console.log("genArray:",words.genArray);
        })
    })
      //.then (callback);
  }

  //Takes object of all possible words for all three slots. Returns an array containing a randomly selected word for each slot
  words.randomizeAll = function () {
    let wordArray = [];
    app.words.genArray.forEach(function(array){
      //Convert string to valid array
      let possibleWords = JSON.parse(array.body);
      let randomNumber = Math.floor(Math.random() * possibleWords.length);
      wordArray.push(possibleWords[randomNumber]);
    });
    console.log('your words are',wordArray);
    return wordArray;
  }

  module.words = words

})(app);
@@ -409,24 +409,88 @@
 ata:%22,data)%0A
+        // TODO: refactor to clear genArray prior to populating%0A
         word
@@ -1056,16 +1056,21 @@
 mNumber%5D
+.word
 );%0A    %7D
@@ -1144,16 +1144,18 @@
 y;%0A  %7D%0A%0A
+%0A%0A
   module

fix bug in randomizeAll function and add TODO in requestWord function