0

Ive got this Random array function that should pick a scope from an array and then pick randomly from the randomly selected scope but it is returning undefined any ideas? I does pick up the file perfectly.

function dynamicgenerator(array, name) {
  //"name" is only for one array
  let arrayPush = [];
  try {

    let arrayJson = require(`../json/${array}`)
    //console.log(arrayJson)//returning true
    for (i in arrayJson) {
      arrayJson.push(arrayPush[`${i}`])
      console.log(arrayPush)

      let randomScope = Math.floor(Math.random() * arrayPush.length); //chooses a scope array out of arrayPush 
      let randomObject = Math.floor(Math.random() * randomScope.length);
      let ret = randomScope[randomObject]
      return ret;
    }
  } catch (e) {
    console.log('DynamicGen returned err whether planned or not.')
    let rand = Math.floor(Math.random() * name.length);
    let ret = name[rand]
    return ret;

  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
ViridianZe
  • 121
  • 12
  • 1
    This is backwards: ```arrayJson.push(arrayPush[`${i}`])``` it should be `arrayPush.push(arrayJson[i])` – Barmar Apr 17 '21 at 16:25
  • 1
    You're returning in the first iteration of the loop. That code should be after the loop. – Barmar Apr 17 '21 at 16:26
  • There are several issues above. 1. The one Barmar pointed out (`arrayJson` where it should be `arrayPush`). 2. There's no reason for ```arrayPush[`${i}`]```, just `arrayPush[i]` is all you need. 3. You don't declare `i` anywhere. 4. `for-in` isn't usually a good choice for looping arrays; see [this answer](https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript) for why and your other, better options. 5. The other one Barmar pointed out (`return` in a loop with no condition will always end the loop on the first pass through). – T.J. Crowder Apr 17 '21 at 16:28
  • 1
    There isn't really any need for `arrayPush` at all -- it's just a copy of `arrayJson`. – Barmar Apr 17 '21 at 16:29
  • `randomScope.length` makes no sense. `randomScope` is a number, it doesn't have a length. – Barmar Apr 17 '21 at 16:30
  • Is `randomScope` supposed to be an element of `arrayPush`? You forgot that part when assigning it. – Barmar Apr 17 '21 at 16:32
  • @Barmar randomScope is meant to pick a random array out of the json to then pick a random object out of that scope – ViridianZe Apr 18 '21 at 04:50

1 Answers1

1
  1. There are a number of problems with the for loop. But this loop is unnecessary. It's just making a needless copy of arrayJson.
  2. You forgot to index the array when assigning randomScope.

function dynamicgenerator(array, name) {
  try {
    let arrayJson = require(`../json/${array}`)
    let randomScope = arrayJson[Math.floor(Math.random() * arrayJson.length)]; //chooses a scope array out of arrayJson 
    let randomObject = Math.floor(Math.random() * randomScope.length);
    let ret = randomScope[randomObject]
    return ret;
  } catch (e) {
    console.log('DynamicGen returned err whether planned or not.')
    let rand = Math.floor(Math.random() * name.length);
    let ret = name[rand]
    return ret;
  }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612