0

Starting from a basic_array, say [0,1,2,3,...48,49,50] how to reorder it using a 6-digits value (say 123456) to produce a new array, i.e something like [48,2,21,0,...50,33,12] (same length + no duplicate values) ?

The goal is to create a specific array for each User identified by these 6 digits. Is there a way to achieve this ?

The value 123456 may be used as is, or as an array [1,2,3,4,5,6] [12,34,56] [123,456]

I tried to turn around ES6 map new Map([...basic_array].map((b,i)=>... but without result.

Wolden
  • 195
  • 1
  • 12

1 Answers1

0

Thanks @Yoshi for suggestion

<input id="code">
<button onclick="createUserArray()">CREATE ARRAY</button>
<button onclick="newUserCode()">NEW CODE</button>

<script>

newUserCode() // create new 6-digits code onload

function newUserCode(){
    let code = Math.floor(Math.random() * 1000000);
    document.getElementById('code').value = addZeros(code)
}

function addZeros(code) { // constant length: adding 0s if needed
    let full_code = code.toString();
    while ( full_code.length < 6 ) { full_code = "0" + full_code };
    return full_code;
}

function createUserArray(){
    let code_user = parseInt( document.getElementById('code').value ), 
        new_array = createArray( code_user );
        console.log(new_array.join(','));
}

function createArray(code) {
  let basic_array = Array.from({length:51},(v,k)=>k)
  let m = basic_array.length, t, i;
  while (m) {
    i = Math.floor(randomSeed(code) * m--);
    t = basic_array[m];
    basic_array[m] = basic_array[i];
    basic_array[i] = t;
    ++code
  }
  return basic_array;
}

function randomSeed(code) {
  let x = Math.sin(code++); 
  return x - Math.floor(x);
}

// 416444 = [9,33,44,38,31,5,37,19,...,23,0,20,18,49,35]
// 663684 = [41,39,4,34,47,31,...1,32,30,46,19,22,0,23]
// 098141 = [48,42,4,34,16,18,...,0,23,21,46,28,31,1,14]
Wolden
  • 195
  • 1
  • 12