Created
January 7, 2016 12:13
-
-
Save SoulFireMage/c89bb054c01e4ef60d57 to your computer and use it in GitHub Desktop.
Very simple example of using mathjax in Javascript
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
<script> | |
//Day 2 learning Javascript | |
//Very simple evaluation of a power rule - note, it's NOT a true evaluation: you can't check the answer correctly in the code this way. | |
function Calculation(){ | |
let base = Math.floor((Math.random() * 100) + 1); | |
let exp = Math.floor((Math.random() * 20) + 1); | |
this.printQuestion = function(){ | |
let calcString = `$$f(x)=${base}x^{${exp}}$$`; | |
document.writeln(calcString); | |
}; | |
this.printAnswer= function(){ | |
let answer = `$$${base * exp}x^{${exp - 1}}$$`; | |
document.getElementById('answer').innerHTML = answer; | |
MathJax.Hub.Queue(["Typeset",MathJax.Hub,'answer']); //must call MathJax on the element! | |
}; | |
} | |
var calc = new Calculation(); | |
calc.printQuestion(); | |
calc.printAnswer(); //called via onClick | |
</script> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Please note this is using es6 standards somewhat :)