Skip to content

Instantly share code, notes, and snippets.

@iAmWillShepherd
Last active October 2, 2017 03:12
Show Gist options
  • Save iAmWillShepherd/65ffba733c6f89a79551e0f7cb0b3eff to your computer and use it in GitHub Desktop.
Save iAmWillShepherd/65ffba733c6f89a79551e0f7cb0b3eff to your computer and use it in GitHub Desktop.
Homework help
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