Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CHALLENGE
- Today your challenge is to write a function that takes in an Array of Arrays, and returns a String which is the markdown representation of a table of that data. Assume the first tuple is the headers for the table.
- Examples
- markdownTable([["name", "email"], ["emily", "emily@email.com"] , ["mary", "maryberry@gbbs.co.uk"]])
- // returns the String below
- Example output (raw):
- |name|email|
- |---|---|
- |emily|emily@email.com|
- |mary|maryberry@gbbs.co.uk|
- ASSUMPTIONS
- 1. The first array entered will be the header information
- 2. Only arrays consisting of two strings will be entered
- TEST CASES
- APPROACH
- First deal with the headers. They need to be printed followed by a "blank line": | --- | --- |. >> Actually I think this needs to be done inside the for loop. So a for loop can be used to loop through the arrays. For each array, print each string separated by piping and a space. Write an if statement to see if it is the first array in the array - if it is, print the blankLine. If not, don't print one.
- CODE
- func markdownTable(groups: [[String]]) -> String {
- let header = groups[0]
- let headerToPrint = "| \(header.joined(separator: " | ")) |"
- //print(headerToPrint)
- let blankLine = "| ---- | ---- |"
- for eachArray in groups {
- if eachArray == groups[0] {
- print("| \(eachArray.joined(separator: " | ")) |\n\(blankLine)")
- } else {
- print("| \(eachArray.joined(separator: " | ")) |")
- }
- }
- print("\(headerToPrint)\n\(blankLine)")
- return "\(headerToPrint)\n\(blankLine)"
- }
- markdownTable(groups: [["name", "email"], ["emily", "emily@email.com"], ["mary", "maryberry@gbbs.co.uk"]])
Add Comment
Please, Sign In to add comment