0

I have 12 arrays named f1 to f12 each with five items. I want to randomly pull one item from each array and push these into the new array sm1.

var sm1 = [];

for (var i=1; i<=12; i++) {
    randomPrompt = Math.floor((Math.random() * 5));
    sm1.push("f"+i[randomPrompt]);
}

However, this returns a random index of "f"+i instead of it indexing f1, f2, f3, f4, etc.

2
  • Any time you find yourself creating numeric variables like that, you should be using an array instead. Commented Jun 4, 2017 at 6:12
  • thanks for the reminder. Yes, in this case, the f1 to f12 variable names are specific to the context I'm working in and cross-reference curriculum content. Using them will allow non-coders who have to tweak this to know which bits to tweak. Commented Jun 4, 2017 at 6:28

5 Answers 5

3

Why don't you use a 2-d array instead of 12 single arrays. Then it will be very easy.

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

Comments

1

Changes below line

sm1.push("f"+i[randomPrompt]);

to

sm1.push(eval("f"+i+"["+randomPrompt+"]"));

Comments

0

There's a few ways to accomplish what you ask.

Example:

var myArray = {
   "f1": [1,2,3,4,5],
   "f2": [6,7,8,9,10],
   "f3": [11,12,13,14,15],
   "f4": [16,17,18,19,20]
}

var sm1 = [];
for (var i=1; i<=4; i++) {
    randomPrompt = Math.floor((Math.random() * 5));
    sm1.push(myArray["f"+i][randomPrompt]);
}
console.log(sm1);

OR, If you are using code in the browser:

f1 = [1,2,3,4,5];
f2 = [6,7,8,9,10];
f3 = [11,12,13,14,15];
f4 = [16,17,18,19,20];

var sm1 = [];

for (var i=1; i<=4; i++) {
    randomPrompt = Math.floor((Math.random() * 5));
    sm1.push(window["f"+i][randomPrompt]);
}
console.log(sm1);

Or use eval():

f1 = [1,2,3,4,5];
f2 = [6,7,8,9,10];
f3 = [11,12,13,14,15];
f4 = [16,17,18,19,20];

var sm1 = [],item;

for (var i=1; i<=4; i++) {
    randomPrompt = Math.floor((Math.random() * 5));
    item = eval(["f"+i][0]);
    sm1.push(item[randomPrompt]);
}
console.log(sm1);

1 Comment

thanks Ali. Very comprehensive. Appreciate you adding different versions as it helps me see other approaches. The second worked fine and most closely followed the code I'm using.
0

You can use the window object.

var sm1 = [];

for (var i=1; i<=12; i++) {
    randomPrompt = Math.floor((Math.random() * 5));
    sm1.push(window["f"+i][randomPrompt]);
}

2 Comments

You can 'this' or current scope in case it is not global.
I'm actually working globally at this point in the code so this is appropriate. I appreciate your warning though @Barmar.
0
var sm1 = [f1, f2, f3, f4, f5, f6, f7, f8, f9, f10, f11, f12].map(function (arr) {
    return arr[Math.floor((Math.random() * arr.length))];   
});

I'd vote using 2-d array instead of named 12 single arrays though.

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.