JSFiddle - React, Tailwind, and code Playground

by jakie8

HTML

<script src="http://laughinghan.github.com/mathquill/mathquill.js"></script>
<div id="results"></dif>

CSS

#results > div{
    margin-bottom:10px;
}

JavaScript

/*
 * polyTexMe
 * @param polynomials: Array
 * @return: Array
 *
 * polyTexMe takes a list of coefficient-arrays, 
 * where the degree of the term is equal to the index
 */
var polyTexMe = function(polynomials){

    var someLatex = [],
        i;
    
    for(i in polynomials){
    
        var coeffArray = polynomials[i],
            rawPoly = [],
            textLine = '',
            order = 0,
            j;
        
        for(j in coeffArray){
        
            var coefficient = coeffArray[j],
                plusSign = '',
                a = '', // will be the coeff for output
                variable = '', // represents the variable factor of the term
                texTerm = [];
            
            if(coefficient != 0){
                if(order > 1){
                    variable = 'x^{'+order+'}';
                } else if(order == 1){
                    variable = 'x';
                } else if(order == 0 && coefficient !=0){
                    a = coefficient;
                }
                
                if(coefficient*coefficient !=1){ // if the coeff is not 1 or -1
                    a = coefficient;
                } else {
                    if(coefficient == -1){
                        if(order > 0){
                            a = '-';
                        } else {
                            a = '-1';
                        }
                    }
                }
            }
            
            // do we need a '+' sign?
            if(coefficient > 0 && order < coeffArray.length-1){ 
                plusSign = '+';
            }
            
            texTerm = [plusSign, a, variable]; // putting this term together
            rawPoly.push(texTerm.join('')); // adding it to the rest of the terms for this polynomial
            order += 1;
        }
        rawPoly.reverse();
        texLine = '\\item $'+(rawPoly.join(''))+'$'; // the actual LaTeX line 
        someLatex.push(texLine);
    }
    return...