After reviewing multiple solutions for finding the longest string in an array, I encountered a new challenge. I am now trying to find the longest string within a nested array. The nesting levels can vary from N-levels deep to just two levels deep. My initial solution is outlined below:
let myArray = [
'ABC',
'ABCD',
'ABCDE',
[
'ABC',
'ABCABABA',
[
'ABABABABABABABABAZZ'
],
'ABCABABASS',
],
'ABCDEFGH',
'ABABABABZZQ'
]
function longestString(arr) {
let longestStr = ''
arr.forEach(item => {
if(typeof item === 'string') {
if(item.length > longestStr.length) {
longestStr = item;
console.log('Longest Item', item);
}
} else {
longestString(item)
}
})
return longestStr;
}
console.log(longestString(myArray))
Current Output -> ABABABABZZQ
Desired Output -> 'ABABABABABABABABAZZ'
What adjustment is necessary to only display the longest string?