Answer to Question #310818 in Java | JSP | JSF for sai

Question #310818

Let variable = [1, [2], [[3]],[[[4]]]]


Output should be: [1, 2, 3, 4]


Dont use flatten function.


Use for loop

1
Expert's answer
2022-03-13T11:22:22-0400
function flattenArray(arr) {
  var result = [];
  
  for( var i=0; i<arr.length; ++i) {
    if (typeof arr[i] === 'number') {
      result.push(arr[i]);
    }
    else {
      result = result.concat(flattenArray(arr[i]));
    }
  }
  
  return result;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS