graphing

2d graphing

by Darby Rathbone

HTML

<input type="text" id='text' value='cos(x)*sin(x/3)'></input>
<br/>
<label id='solution'></label>
<canvas id='c'></canvas>
<script>
    
</script>

CSS

canvas {
    z-index:-1;
    outline:double black 3px;
    position:fixed;
    width:100%;
    height:100%;
    top:0px;
    bottom:0px;
    left:0px;
    right:0px;
}
html, body {
    margin:0px;
    padding:0px;
    width:100%;
    height:100%;
}

JavaScript

var numbers = /([^0-9\.]*[0-9\.]+)/gi;



var parens = /\(([^\(\)]+)\)/gmi;
var solve = function (s) {
    var num1 = "";
    var nums = s.split(numbers).filter(Boolean);
    var actions = [];
    nums = nums.map(function (e, i, a) {
        e = e.split("");
        if (i !== 0) actions.push(e.splice(0, 1));

        e = e.join('');
        e = e.replace(/\-+/g, function (b) {
            return b.length % 2 ? "-" : "";
        });
        return e;
    });
    nums = nums.map(function (e, i, a) {
        if (actions[i] == "^") {
            var b = a[i + 1];
            //console.log(e+'^'+a[i+1]);
            a[i + 1] = "";
            actions[i] = "";
            return Math.pow(parseFloat(e), parseFloat(b));

        }
        return e;
    }).filter(function (e) {
        return Boolean(new String(e).trim());
    });
    actions = actions.filter(function (e) {
        return Boolean(new String(e).trim());
    });
    nums = nums.map(function (e, i, a) {
        if (actions[i] == "*") {
            var b = a[i + 1];
            //console.log(e+'*'+a[i+1]);
            a[i + 1] = "";
            actions[i] = "";
            return (parseFloat(e) * parseFloat(b));;

        }
        if (actions[i] == "/") {
            var b = a[i + 1];
            //console.log(e+'/'+a[i+1]);
            a[i + 1] = "";
            actions[i] = "";
            return (parseFloat(e) / parseFloat(b));

        }
        return e;
    }).filter(function (e) {
        return Boolean(new String(e).trim());
    });
    actions = actions.filter(function (e) {
        return Boolean(new String(e).trim());
    });
    nums = nums.map(function (e, i, a) {
        if (actions[i] == "+") {
            var b = a[i + 1];
            //console.log(a);
            //console.log(a[i]+' + '+a[i+1]);
            a[i + 1] = (parseFloat(e) + parseFloat(b));
            actions[i] = "";
            return '';

        }
        if (actions[i] == "-") {
            var b = a[i + 1];
           ...