---
title: "Comment by D on \"Javascript/Frontend DS&A Interview questions\" | Hashnode"
description: "Here&#39;s my solution with maxDepth: function flatten ( arr, maxDepth=Infinity, currDepth = 1 ) { let res = [] for (let i = 0 ; i &lt; arr. length ; i + + ) {"
url: "https://hashnode.com/posts/javascript-dsa-interview-questions/61141311611d5c199a2fae00/comment/6298d029c9d0c9130286ea55"
---

[Discussion](https://hashnode.com/posts/javascript-dsa-interview-questions/61141311611d5c199a2fae00)

Comment [Javascript/Frontend DS&A Interview questions](https://hashnode.com/posts/javascript-dsa-interview-questions/61141311611d5c199a2fae00)

[D](https://hashnode.com/@dmtri)

[Jun 2, 2022](https://hashnode.com/posts/javascript-dsa-interview-questions/61141311611d5c199a2fae00/comment/6298d029c9d0c9130286ea55)

Here's my solution with maxDepth:

```
function flatten(arr, maxDepth=Infinity, currDepth = 1){
    let res = []
    for (let i = 0; i < arr.length; i++) {
        if (!Array.isArray(arr[i]) || currDepth > maxDepth) {
            res.push(arr[i])
        } else {
            res = res.concat(flatten(arr[i], maxDepth, ++currDepth))
        }
    }
    return res
}
```

[Evleensingh Thakral](https://hashnode.com/@evleen13)

[Nov 15, 2022](https://hashnode.com/posts/javascript-dsa-interview-questions/61141311611d5c199a2fae00/comment/6298d029c9d0c9130286ea55#reply-6373afa5747bca51e27c9a55)

Awesome
