Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function flattenArray(array) {
- return array.reduce((a, b) => Array.isArray(b) ? a.concat(flattenArray(b)) : a.concat(b), []);
- }
- // Edit these to change the test parameters.
- let numberOfTests = 1000000
- let lengthOfArrays = 20;
- let maximumNumber = Number.MAX_SAFE_INTEGER
- // vvvvvvvvvvvvvvvvvvvvvvvv TESTS vvvvvvvvvvvvvvvvvvvvvvvv
- let passed = true;
- for (let i = 0; i < numberOfTests; i++) {
- // Create the arrays to test.
- let testArrays = createNestedArray(lengthOfArrays, maximumNumber);
- // Test the flattenArray function.
- let actual = flattenArray(testArrays.nested);
- // Check if the test failed.
- if (!isEqualArrays(testArrays.flattened, actual)) {
- console.log('Tests failed!\n\nExpected:', testArrays.flattened, '\nActual:', actual, '\nTests run:', i + 1);
- passed = false;
- break;
- }
- }
- // Check if all the tests passed.
- if (passed) {
- console.log(numberOfTests + ' tests passed!');
- }
- // Simple function to check for shallow equality of two arrays.
- function isEqualArrays(array1, array2) {
- return array1.every((ele, index) => ele === array2[index]);
- }
- // The actual testing function that builds both the nested and flattened arrays.
- function createNestedArray(length, maxNumber) {
- let outputArray = {
- nested: [],
- flattened: []
- };
- for (let i = 0; i < length; i++) {
- // Generate a random number to use as the next element in the arrays.
- let element = Math.ceil(Math.random() * maxNumber);
- // Generate a random number to select which option to take for the nested array.
- let selector = Math.random();
- // Add the element to the flat array.
- outputArray.flattened.push(element);
- // Short curcuit the loop on the first iteration so that we don't try to put something at the -1 index.
- if (outputArray.nested.length === 0) {
- selector >= 0.5 ? outputArray.nested.push([element]) : outputArray.nested.push(element);
- continue;
- }
- if (selector >= 0.75) {
- // Put the element at the end of the main array but inside its own array.
- outputArray.nested.push([element]);
- } else if (selector >= 0.5) {
- // If the previous element in the main array is an array itself.
- if (Array.isArray(outputArray.nested[outputArray.nested.length - 1])) {
- // Put the element inside that array.
- outputArray.nested[outputArray.nested.length - 1].push(element);
- } else {
- // Put both the previous element and the current element inside an array in place of the previous element.
- outputArray.nested[outputArray.nested.length - 1] = [outputArray.nested[outputArray.nested.length - 1], element];
- }
- } else if (selector >= 0.25) {
- // If the previous element in the main array is an array itself.
- if (Array.isArray(outputArray.nested[outputArray.nested.length - 1])) {
- // Put the element inside that array but in it's own array.
- outputArray.nested[outputArray.nested.length - 1].push([element]);
- } else {
- // 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.
- outputArray.nested[outputArray.nested.length - 1] = [outputArray.nested[outputArray.nested.length - 1], [element]];
- }
- } else {
- // Push element onto the end of the main array.
- outputArray.nested.push(element);
- }
- }
- return outputArray;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement