Last active
December 13, 2023 15:27
-
-
Save ronaldroe/8ac6ad5861e7650e52a27a04aa705ef8 to your computer and use it in GitHub Desktop.
Revisions
-
ronaldroe revised this gist
Dec 13, 2023 . 1 changed file with 0 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,3 @@ // This test takes an array, loops through each top level index // and logs an error if the index contains an array. // Passes are tallied, and passes/fails are output at the end of the test. -
ronaldroe revised this gist
Mar 10, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,7 @@ const flatten = arr => { // If the current item is an array, recurse into it. // It's usually better to not use recursion, but in this case, // depth is unknown, and recursion makes sense. } else { // Spread the results of the recursion into the output array. -
ronaldroe revised this gist
Mar 9, 2020 . No changes.There are no files selected for viewing
-
ronaldroe created this gist
Mar 9, 2020 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,35 @@ // Decided against a testing framework for something so simple. // This test takes an array, loops through each top level index // and logs an error if the index contains an array. // Passes are tallied, and passes/fails are output at the end of the test. const flattenTest = arr => { // Define error message and initialize pass counter const errorMessage = ind => `Index ${ind} contains an array`; let passCount = 0; arr.forEach((item, ind) => { // If the item is an array, log the message if(Array.isArray(item)){ console.error(errorMessage(ind)); // If not an array, increment pass counter } else { passCount++; } }); // Log out results console.log(`${arr.length} total top level indexes.`); console.log(`${passCount} tests passed.`); console.log(`${arr.length - passCount} tests failed.`); } module.exports = flattenTest; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ const flatten = arr => { let outputArr = []; // Loop through each index of the array arr.forEach(item => { // If the current item is not an array, push its value immediately. if(!Array.isArray(item)){ outputArr.push(item); // If the current item is an array, recurse into it. // It's usually better to not use recursion, but in this case, // it makes it easier to continue to the next level. } else { // Spread the results of the recursion into the output array. outputArr.push(...flatten(item)); } }); return outputArr; } // Created for use with Node or other CommonJS module.exports = flatten; This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ // Run this file using `node index.js` to see results. const flattenTest = require('./flatten-test.js'); const flatten = require('./flatten.js'); const arr = [[1,2,[3]],4]; // First, test flatten function console.log('Running example array with flatten function'); flattenTest(flatten(arr)); // Second, show test failures using input array console.log('\nRunning example array without flatten function') flattenTest(arr);