Skip to content

Instantly share code, notes, and snippets.

@SoulFireMage
Created January 7, 2016 12:13
Show Gist options
  • Save SoulFireMage/c89bb054c01e4ef60d57 to your computer and use it in GitHub Desktop.
Save SoulFireMage/c89bb054c01e4ef60d57 to your computer and use it in GitHub Desktop.
Very simple example of using mathjax in Javascript
<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>
@SoulFireMage
Copy link
Author

Please note this is using es6 standards somewhat :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment