How to convert a two-dimensional array to CSV format using a function and return a string?

I need to write a function that converts a two-dimensional array to CSV and returns a string. Valid values ​​as array elements are numbers and strings If a function is encountered, throw an error with the text “Unexpected value”

Example:

func([[1, 2], ['a', 'b']]) // '1,2 a,b' 
func([[1, 2], ['a,b', 'c,d']]) // '1,2 "a,b","c,d"'

I did this, the test “correctly escapes quotes” fails:

Expected: """"text""","other ""long"" text"" 
Received: ""text",other "long" text"

My code:

function func(arr) 
{ return arr     
.map(array => array.map(e => {         
let type = typeof e;         
if (type !== "number" && type !== "string")             
throw new Error("Unexpected value");         
return (type === "string" && e.includes(",")) ? JSON.stringify(e) : e;     
}).join(","))     
.join("\n"); 
}

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.