Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. function flattenArray(array) {
  2. return array.reduce((a, b) => Array.isArray(b) ? a.concat(flattenArray(b)) : a.concat(b), []);
  3. }
  4.  
  5. // Edit these to change the test parameters.
  6. let numberOfTests = 1000000
  7. let lengthOfArrays = 20;
  8. let maximumNumber = Number.MAX_SAFE_INTEGER
  9.  
  10. // vvvvvvvvvvvvvvvvvvvvvvvv TESTS vvvvvvvvvvvvvvvvvvvvvvvv
  11.  
  12. let passed = true;
  13.  
  14. for (let i = 0; i < numberOfTests; i++) {
  15. // Create the arrays to test.
  16. let testArrays = createNestedArray(lengthOfArrays, maximumNumber);
  17.  
  18. // Test the flattenArray function.
  19. let actual = flattenArray(testArrays.nested);
  20.  
  21. // Check if the test failed.
  22. if (!isEqualArrays(testArrays.flattened, actual)) {
  23. console.log('Tests failed!\n\nExpected:', testArrays.flattened, '\nActual:', actual, '\nTests run:', i + 1);
  24. passed = false;
  25. break;
  26. }
  27. }
  28.  
  29. // Check if all the tests passed.
  30. if (passed) {
  31. console.log(numberOfTests + ' tests passed!');
  32. }
  33.  
  34. // Simple function to check for shallow equality of two arrays.
  35. function isEqualArrays(array1, array2) {
  36. return array1.every((ele, index) => ele === array2[index]);
  37. }
  38.  
  39. // The actual testing function that builds both the nested and flattened arrays.
  40. function createNestedArray(length, maxNumber) {
  41. let outputArray = {
  42. nested: [],
  43. flattened: []
  44. };
  45.  
  46. for (let i = 0; i < length; i++) {
  47. // Generate a random number to use as the next element in the arrays.
  48. let element = Math.ceil(Math.random() * maxNumber);
  49. // Generate a random number to select which option to take for the nested array.
  50. let selector = Math.random();
  51. // Add the element to the flat array.
  52. outputArray.flattened.push(element);
  53.  
  54. // Short curcuit the loop on the first iteration so that we don't try to put something at the -1 index.
  55. if (outputArray.nested.length === 0) {
  56. selector >= 0.5 ? outputArray.nested.push([element]) : outputArray.nested.push(element);
  57. continue;
  58. }
  59.  
  60. if (selector >= 0.75) {
  61. // Put the element at the end of the main array but inside its own array.
  62. outputArray.nested.push([element]);
  63. } else if (selector >= 0.5) {
  64. // If the previous element in the main array is an array itself.
  65. if (Array.isArray(outputArray.nested[outputArray.nested.length - 1])) {
  66. // Put the element inside that array.
  67. outputArray.nested[outputArray.nested.length - 1].push(element);
  68. } else {
  69. // Put both the previous element and the current element inside an array in place of the previous element.
  70. outputArray.nested[outputArray.nested.length - 1] = [outputArray.nested[outputArray.nested.length - 1], element];
  71. }
  72. } else if (selector >= 0.25) {
  73. // If the previous element in the main array is an array itself.
  74. if (Array.isArray(outputArray.nested[outputArray.nested.length - 1])) {
  75. // Put the element inside that array but in it's own array.
  76. outputArray.nested[outputArray.nested.length - 1].push([element]);
  77. } else {
  78. // Put both the previous element and the current element inside an array in place of the previous element, but put the current element inside it's own array.
  79. outputArray.nested[outputArray.nested.length - 1] = [outputArray.nested[outputArray.nested.length - 1], [element]];
  80. }
  81. } else {
  82. // Push element onto the end of the main array.
  83. outputArray.nested.push(element);
  84. }
  85. }
  86.  
  87. return outputArray;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement