js/qa-generator.js
/**
     * Generates equation type for a question
     * @method generateEquation
     * @param  {EquationType}  type of equation
     */
Generates equation type for a question
@method generateEquation
@param  {EquationType}  type of equation
Generates equation type for a question @method generateEquation @param { EquationType } type of equation
function generateEquation(item, EquationType, useFractions) {
      var expr = undefined;
      var expr2 = undefined;
      var expr3 = undefined;
      var number = undefined;
      var equation = undefined;
      var solution = undefined;
      switch (EquationType) {
        case "basic":
          if (useFractions === true) {            
            number = new Fraction(1, randomNum(2*maxValue));
          } else {
            number = randomNum(maxValue);
          }
          expr = new Expression(item);         
          expr = expr.multiply(randomNum(maxValue)).multiply(randomNum(maxValue));
          expr = expr.multiply(2);
          expr = expr.multiply(number);
          equation = new Equation(expr, 3 + randomNum(3*maxValue));
          try {
            solution = equation.solveFor(item);
          } catch(err) {
            equation = generateEquation(item, EquationType, useFractions)
            solution = equation.solveFor(item);
          }          
          break;
        case "intermediate":
          var operations = ["+", "-", "*"];
          if (useFractions === true) {            
            number = new Fraction(3, randomNum(2*maxValue));
          } else {
            number = randomNum(maxValue);
          }
          expr = new Expression(item);         
          expr = randomOperation(operations, expr, maxValue);
          expr = expr.multiply(number);
          expr = randomOperation(operations, expr, maxValue);
          expr = randomOperation(operations, expr, maxValue);
          equation = new Equation(expr, 1 + randomNum(2*maxValue));
          try {
            solution = equation.solveFor(item);
          } catch(err) {
            equation = generateEquation(item, EquationType, useFractions)
            solution = equation.solveFor(item);
          }          
          break;
        case "advanced":
          var operations = ["+", "-"];
          expr = new Expression(item);
          if (useFractions === true) {            
            number = new Fraction(1, randomNum(2*maxValue));
          } else {
            number = randomNum(maxValue);
          }          
          expr = randomOperation(operations, expr, maxValue);
          expr = randomOperation(operations, expr, maxValue);
          expr = expr.add(1 + randomNum(maxValue));
          expr = expr.multiply(1 + randomNum(maxValue));
          expr = expr.multiply(number);
          expr2 = new Expression(item);
          expr2 = randomOperation(operations, expr, maxValue);
          expr2 = randomOperation(operations, expr, maxValue);
          expr2 = expr2.multiply(number);
          expr = expr.simplify();
          expr2 = expr2.simplify();
          equation = new Equation(algebra.parse(expr.toString()), algebra.parse(expr2.toString()));
          try {
            solution = equation.solveFor(item);
          } catch(err) {
            equation = generateEquation(item, EquationType, useFractions)
            solution = equation.solveFor(item);
          }          
          break;
      }
      if (solution.toString() === "0") {
        equation = generateEquation(item, EquationType, useFractions);
      }
      if (solution.toString() === "1") {
        equation = generateEquation(item, EquationType, useFractions);
      }
      return equation;
    }
function generateEquation ( item , EquationType , useFractions ) { var expr = undefined ; var expr2 = undefined ; var expr3 = undefined ; var number = undefined ; var equation = undefined ; var solution = undefined ; switch ( EquationType ) { case "basic" : if ( useFractions === true ) { number = new Fraction ( 1 , randomNum ( 2 * maxValue ) ) ; } else { number = randomNum ( maxValue ) ; } expr = new Expression ( item ) ; expr = expr . multiply ( randomNum ( maxValue ) ) . multiply ( randomNum ( maxValue ) ) ; expr = expr . multiply ( 2 ) ; expr = expr . multiply ( number ) ; equation = new Equation ( expr , 3 + randomNum ( 3 * maxValue ) ) ; try { solution = equation . solveFor ( item ) ; } catch ( err ) { equation = generateEquation ( item , EquationType , useFractions ) solution = equation . solveFor ( item ) ; } break ; case "intermediate" : var operations = [ "+" , "-" , "*" ] ; if ( useFractions === true ) { number = new Fraction ( 3 , randomNum ( 2 * maxValue ) ) ; } else { number = randomNum ( maxValue ) ; } expr = new Expression ( item ) ; expr = randomOperation ( operations , expr , maxValue ) ; expr = expr . multiply ( number ) ; expr = randomOperation ( operations , expr , maxValue ) ; expr = randomOperation ( operations , expr , maxValue ) ; equation = new Equation ( expr , 1 + randomNum ( 2 * maxValue ) ) ; try { solution = equation . solveFor ( item ) ; } catch ( err ) { equation = generateEquation ( item , EquationType , useFractions ) solution = equation . solveFor ( item ) ; } break ; case "advanced" : var operations = [ "+" , "-" ] ; expr = new Expression ( item ) ; if ( useFractions === true ) { number = new Fraction ( 1 , randomNum ( 2 * maxValue ) ) ; } else { number = randomNum ( maxValue ) ; } expr = randomOperation ( operations , expr , maxValue ) ; expr = randomOperation ( operations , expr , maxValue ) ; expr = expr . add ( 1 + randomNum ( maxValue ) ) ; expr = expr . multiply ( 1 + randomNum ( maxValue ) ) ; expr = expr . multiply ( number ) ; expr2 = new Expression ( item ) ; expr2 = randomOperation ( operations , expr , maxValue ) ; expr2 = randomOperation ( operations , expr , maxValue ) ; expr2 = expr2 . multiply ( number ) ; expr = expr . simplify ( ) ; expr2 = expr2 . simplify ( ) ; equation = new Equation ( algebra . parse ( expr . toString ( ) ) , algebra . parse ( expr2 . toString ( ) ) ) ; try { solution = equation . solveFor ( item ) ; } catch ( err ) { equation = generateEquation ( item , EquationType , useFractions ) solution = equation . solveFor ( item ) ; } break ; } if ( solution . toString ( ) === "0" ) { equation = generateEquation ( item , EquationType , useFractions ) ; } if ( solution . toString ( ) === "1" ) { equation = generateEquation ( item , EquationType , useFractions ) ; } return equation ; }
Generates equation type for a question
 @method generateEquation
 @param  {EquationType}  type of equation
Generates equation type for a question @method generateEquation @param { EquationType } type of equation
//console.log(EquationType); //console.log(useFractions); // [ 3x = 12 ] //expr = expr.multiply(3); //expr = expr.simplify(); // [ 4x - 3 = 13 ] // [ 5x + 3 = 3x + 15 ] //console.log("......................."); //console.log(equation.toString()); //console.log(solution.toString()); //console.log("......................."); // rebuild // rebuild