Skip to content

Commit

Permalink
refactor: improving readability for flat function
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaban-Eissa committed May 26, 2024
1 parent 5264f82 commit 4c386b7
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions 3.implement-Array.prototype.flat.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ Are you able to solve it both recursively and iteratively?
* @param { Array } arr
* @param { number } depth
*/
function flat(arr, depth = 1) {
const flat = (array, depth = 1) => {
let flatArray = [];
for (const item of arr) {
if (Array.isArray(item) && depth > 0) {
flatArray = flatArray.concat(flat(item, depth - 1));
array.forEach((element) => {
if (Array.isArray(element) && depth > 0) {
flatArray.push(...flat(element, depth - 1));
} else {
flatArray.push(item);
flatArray.push(element);
}
}
});
return flatArray;
}
};
```

#
Expand Down

0 comments on commit 4c386b7

Please sign in to comment.