im trying to do some Math in a js function while using MathJs & getting this error :Math.evaluate is not a function
<script src=" https://cdnjs.com/libraries/mathjs"></script>
Do-Math :<input id="formula" />
<button onclick="goCalc()">Go</button>
results:<input id="resTxt"/>
<script type="text/javascript">
function goCalc() {
let Mformula = $("#formula").val();//the formula im trying to get =6*4+2^2
var res = document.getElementById("resTxt");
// res.setAttribute("value", Mformula);
var mat = Math.evaluate(Mformula);
res.setAttribute(mat);
console.log(mat);
}
It looks like you might be experiencing multiple problems based on the code you've shown. It's not exactly clear which are true issues and which might be related to typos, etc. so here's a complete example demonstrating how to include the library as well as evaluating the text value of an input element each time it changes, writing the result to another element:
body { font-family: sans-serif; } #expression, #result { font-family: monospace; font-size: 1rem; } #expression { padding: 0.5rem; }
<label>Expression: <input id="expression" value="6 * 4 + 2 ^ 2" /></label>
<pre><code id="result"></code></pre>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/11.0.1/math.min.js" integrity="sha512-B82WLflI1EQiR9sGbIV1ddGVvK4ghj1xjMShL7YvcOrHjX2qP72lHztT1DxBVPiz1aTR6mOUJbtwj06uadL2GA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script type="module">
const input = document.getElementById('expression');
const output = document.getElementById('result');
function evaluate () {
const expression = input.value.trim();
let result;
try {
result = math.evaluate(expression);
}
catch (ex) {
if (ex instanceof Error) result = ex.toString();
else throw ex;
}
output.textContent = result;
}
input.addEventListener('input', evaluate);
evaluate();
</script>