Last active
October 2, 2017 03:12
-
-
Save iAmWillShepherd/65ffba733c6f89a79551e0f7cb0b3eff to your computer and use it in GitHub Desktop.
Homework help
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createTableFromStr(str, maxCols) { | |
var tokens = str.split(" ") | |
var rows = [] | |
var cols = [] | |
for (var i = 0; i < tokens.length; i += 1) { | |
var currToken = tokens[i] | |
var td = `<td>${currToken}</td>` | |
cols.push(td) | |
if (cols.length === maxCols) { | |
var tr = `<tr>${cols.join("")}</tr>` | |
rows.push(tr) | |
cols = [] | |
} | |
} | |
if (cols.length > 0 && cols.length <= maxCols) { | |
var numNullsToAdd = maxCols - cols.length | |
for (var i = 0; i < numNullsToAdd; i++) { | |
cols.push(`<td>null</td>`) | |
} | |
var tr = `<tr>${cols.join("")}</tr>` | |
rows.push(tr) | |
} | |
return `<table><tbody>${rows.join("")}</tbody></table>` | |
} | |
var test1 = createTableFromStr("Elizabeth thinks she's awesome", 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment