-1

You need to write a function that translates a two-dimensional array (array of arrays) into CSV format and return a string.

My code:

console.log(arraysToCsv([[1, 2], ['a', 'b']])) 
console.log(arraysToCsv([[1, 2], ['a,b', 'c,d']])) 

function arraysToCsv(data) {
  for (let i = 0; i < data.length; i++) {
    if (data[i] == 'number') {
      return data[i];
    }
    if (data[i] == 'string') {
      return `'${data[i].join('')}'`
    }
    return data[i].join();
  }
}

Checks do not pass for some reason. How do I separate numbers and strings, strings should be in quotation marks?

Alla Max
  • 9
  • 6
  • Does this answer your question? [Javascript code to parse CSV data](https://stackoverflow.com/questions/1293147/javascript-code-to-parse-csv-data) – Bhavik Hirani Apr 20 '20 at 09:31
  • difficult to understand – Alla Max Apr 20 '20 at 09:37
  • SO is not a homework-doing service! Please ask a specific question on a problem you are encountering. "It doesn't work... make it" is not what SO is for. Please tell us what you have tried, what doesn't work and what question you want to ask specifically. – Geoff James Apr 20 '20 at 10:09

1 Answers1

0

You're testing the value of data[i], not its type.

Replace

if (data[i] == 'number')

with

if (typeof data[i] == 'number')
Jeremy Thille
  • 26,047
  • 12
  • 43
  • 63