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;
}
}
arrayJson.push(arrayPush[`${i}`])it should bearrayPush.push(arrayJson[i])arrayJsonwhere it should bearrayPush). 2. There's no reason forarrayPush[`${i}`], justarrayPush[i]is all you need. 3. You don't declareianywhere. 4.for-inisn'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 (returnin a loop with no condition will always end the loop on the first pass through).arrayPushat all -- it's just a copy ofarrayJson.randomScope.lengthmakes no sense.randomScopeis a number, it doesn't have a length.