fork download
  1. function shuffleArray(array) {
  2. for (let i = array.length - 1; i > 0; i--) {
  3. const j = Math.floor(Math.random() * (i + 1));
  4. [array[i], array[j]] = [array[j], array[i]];
  5. }
  6. return array;
  7. }
  8.  
  9. function shufflePartial(data) {
  10. function foo(lst) {
  11. lst = shuffleArray(lst);
  12. return lst.map(item => item[0]).concat(lst.findIndex(item => item[1]) + 1);
  13. }
  14. return [data[0]].concat(data.slice(1).map(k => {
  15. const processed = [[k[0]]].concat(k.slice(1, -1).map((item, index) => [item, k[k.length - 1] === index + 1]));
  16. return [k[0]].concat(foo(processed.slice(1)));
  17. }));
  18. }
  19.  
  20. // Example usage:
  21. const data = [
  22. ['Japanese', 'Choice1', 'Choice2', 'Choice3', 'Choice4', 'Answer'],
  23. ['個人・個々の', 'opposite', 'microscopic', 'individual', 'separate', 3],
  24. ['必要な', 'necessity', 'necessarily', 'need', 'necessary', 4]
  25. ];
  26.  
  27. console.log(shufflePartial(data));
  28.  
  29.  
Success #stdin #stdout 0.04s 17236KB
stdin
Standard input is empty
stdout
Japanese,Choice1,Choice2,Choice3,Choice4,Answer,個人・個々の,opposite,separate,individual,microscopic,3,必要な,necessity,necessary,necessarily,need,2