Skip to content

Instantly share code, notes, and snippets.

@Himujjal
Last active November 24, 2017 17:06
Show Gist options
  • Save Himujjal/25a472a303755158f50ae69b5f95f87c to your computer and use it in GitHub Desktop.
Save Himujjal/25a472a303755158f50ae69b5f95f87c to your computer and use it in GitHub Desktop.
Making a function which takes in an array and then creates a table out of it
<html>
<head>
<title>Simple Linear Regression</title>
</head>
<body style="text-align:center;">
<h1>Simple Linear Regression</h1>
<table id="original-dataset"></table>
</body>
<script src="simple-regression-A.js"></script>
</html>
let bills = [34,51,64,88,99,108]
let tips = [5,5,11,8,14,17]
let headers = ["Bills", "Tips"]
let transpose = (arr) => Object.keys(arr[0]).map( (c) => arr.map( (r) => r[c] ) )
function fillTable (tableHeaders, mat, tableId) {
let matrix = transpose(mat)
let headRow = document.createElement('tr')
tableHeaders.forEach(head=>{
let col = document.createElement('th')
col.appendChild(document.createTextNode(head))
headRow.appendChild(col)
})
document.getElementById(tableId).appendChild(headRow)
matrix.forEach((ele,i)=>{
let row = document.createElement('tr')
let cols = [...Array(matrix[0].length)].map(ele=>document.createElement('td'))
cols.forEach((col,j) => {
col.appendChild(document.createTextNode(matrix[i][j]))
row.appendChild(col)
})
document.getElementById(tableId).appendChild(row)
})
}
// This line is what matters. Ignore the above if required.
fillTable(headers, [bills, tips], "original-dataset") // This will fill up the table automatically
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment