Skip to content

Instantly share code, notes, and snippets.

@camerican
Last active October 12, 2016 15:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save camerican/b7b845e6017ceb61b3f57966ca610ecf to your computer and use it in GitHub Desktop.
Save camerican/b7b845e6017ceb61b3f57966ca610ecf to your computer and use it in GitHub Desktop.
Example of a simple function calculator w/ output to HTML Table
let elM, elC, elP, elR, points;
let min = -10
let max = 10;
document.addEventListener("DOMContentLoaded",function(){
elM = document.getElementById("m");
elC = document.getElementById("c");
elP = document.getElementById("p");
elR = document.getElementById("result");
document.getElementById("run").addEventListener("click",calculate);
});
function functionResolver(m, c, p) {
let points = [];
for(let x=min; x<=max; x++) {
points.push( [x,m * ( x * x ) + c * x + p] )
}
return points;
}
function calculate() {
points = functionResolver(+elM.value,+elC.value,+elP.value);
//wipe result table, creating col headings
elR.innerHTML = "<tr><th>x</th><th>y</th></tr>";
points.forEach( coord => {
elR.innerHTML += `<tr><td>${coord[0]}</td><td>${coord[1]}</td></tr>\n`;
});
}
<input id="m" />
<input id="c" />
<input id="p" />
<button id="run">
Run Function
</button>
<table id="result">
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment