array.join method doos not give space in out put in case of nested array

Viewed 30

function flattenArray(a) {
  // your code starts here.
  let c = a.join(", ");
  return c
}

const nestedNums = [5, 6, [7], ["8", ["9", ["10" ]]]];
const flatNums = flattenArray(nestedNums);
console.log(flatNums); 

OutPut of About code is 5, 6, 7, 8,9,10

My question is that why there is no space after 8 & 9

1 Answers
[5, 6, [7], ["8", ["9", ["10" ]]]].flat(Infinity).join(', ')

You can try something like this, flat(Infinity) flatten all nested arrays.

Related