This content originally appeared on DEV Community and was authored by Randy Rivera
- I've been saving these posts for a while now. I would always want to post something that I myself understand as well. Well Today we have a problem that needs us to flatten a nested array. We should also account for different levels of nesting.
- Code:
function checkForArrayApproval(arr) {
return arr;
}
checkForArrayApproval([1, [2], [3, [[4]]]]);
- Answer:
function steamrollArray(arr) {
let newArr = []
// Loop over array contents
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
// Recursively flatten entries that are arrays
// and push into the newArr
newArr.push(...steamrollArray(arr[i]));
} else {
// Copy contents that are not arrays
newArr.push(arr[i]);
}
}
return newArr;
}
// we could also do it this way:
// while (arr.some(function(num) {
// return Array.isArray(num)
// })) {
// let i = 0
// arr = arr.flat()
// i += 1
// }
// return arr;
// }
console.log(steamrollArray([1, [2], [3, [[4]]]])); // will display [1, 2, 3, 4]
Binary Code
- In this following post we must return an English translated sentence of the passed binary string.
- Code:
function binaryCode(str) {
return str;
}
binaryCode("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111");
- Answer:
function binaryCode(str) {
return str.split(" ").map(dataPoint => {
let characterPoint = parseInt(dataPoint, 2);
let decipheredLetter = String.fromCharCode(characterPoint);
return decipheredLetter;
}).join("")
}
console.log(binaryCode("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111")); will display Aren't bonfires fun!?
// 2 Binary numeral system Used internally by nearly all computers, is base 2. The two digits are "0" and "1", expressed from switches displaying OFF and ON, respectively.
// The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.
// Ex:
// const array1 = [1, 4, 9, 16];
// // pass a function to map
// const map1 = array1.map(x => x * 2);
// console.log(map1);
// // expected output: Array [2, 8, 18, 32];
It is a sunday, currently 4:15 in the afternoon, today we're going to flatten a nested array.
Also don't forget that you must consider for different levels of nesting.
Code:
function nestedArr(arr) {
return arr;
}
nestedArr([1, [2], [3, [[4]]]]);
- Answer:
function nestedArr(arr) {
let newArr = []
for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) { // wants to check each element if it is an Array or not.
newArr = newArr.concat(nestedArr(arr[i])) // we're using recursion because we don't know how many levels it will be so we want it to call itself whenever it is needed. call for each array, if it is we go deeper in the nested levels
} else {
newArr.push(arr[i]) // if it is not an array (base case)
}
}
return newArr;
}
console.log(nestedArr([1, [2], [3, [[4]]]])); will display [1, 2, 3, 4]
This content originally appeared on DEV Community and was authored by Randy Rivera
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.
APA
MLA
Randy Rivera | Sciencx (2021-08-22T20:35:29+00:00) Flatten a nested array. Algorithms and Scripting. Retrieved from https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/
" » Flatten a nested array. Algorithms and Scripting." Randy Rivera | Sciencx - Sunday August 22, 2021, https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/
HARVARDRandy Rivera | Sciencx Sunday August 22, 2021 » Flatten a nested array. Algorithms and Scripting., viewed ,<https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/>
VANCOUVERRandy Rivera | Sciencx - » Flatten a nested array. Algorithms and Scripting. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/
CHICAGO" » Flatten a nested array. Algorithms and Scripting." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/
IEEE" » Flatten a nested array. Algorithms and Scripting." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/. [Accessed: ]
rf:citation » Flatten a nested array. Algorithms and Scripting | Randy Rivera | Sciencx | https://www.scien.cx/2021/08/22/flatten-a-nested-array-algorithms-and-scripting/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.