I’m have beginner skills and am hoping someone can help me with a complex script in JavaScript ECMA-262 (as basic a JS script as possible, since the program I am using has only older implementation of ECMA-262. So some some array functions like “splice” even “concat” are not found in the language reference for this program. But basic JS works OK).
I am trying to create an array totaling 6 numbers from 2 separate subarrays (subarrayA=1,2,3 and subarrayB=4,5,6), alternately placing a number (chosen randomly from each subarray) from each subarray into the final 6 item array. Also, no duplicates.
So, for example, 3,6,1,4,2,5.
I’ve tried online searching, this board, trying out zipper methods from 2 subarrays, % to pick out odd/even, etc. So far, nothing has worked. But at least I’ve gotten some more knowledge allowing me to ask the question in a way that may help create a more specific solution. I would hard code this, but I think that the number of possible variations/cases may be quite large?
I am hoping someone can suggest a script/function in basic JS that can (1) set up each random subarray, then (2) create the final 6 item array.
Kind Regards,
If you’re restricted to a sub-set of the modern JS API, I would probably take the approach of first merging the two arrays (iteratively) then applying a random sort. Something like:
arr1is now an array of the 6 numbers in random order, no repeats.[EDIT] – based on the discussion below, try this.