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;

  }
}
7
  • 1
    This is backwards: arrayJson.push(arrayPush[`${i}`]) it should be arrayPush.push(arrayJson[i]) Commented Apr 17, 2021 at 16:25
  • 1
    You're returning in the first iteration of the loop. That code should be after the loop. Commented Apr 17, 2021 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 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). Commented Apr 17, 2021 at 16:28
  • 1
    There isn't really any need for arrayPush at all -- it's just a copy of arrayJson. Commented Apr 17, 2021 at 16:29
  • randomScope.length makes no sense. randomScope is a number, it doesn't have a length. Commented Apr 17, 2021 at 16:30

1 Answer 1

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;
  }
}

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.