$x^2 - 3x + 2$

$= x^2 - x - 2x + 2$

$= x(x - 1) - 2(x - 1)$

$= (x - 1)(x - 2)$

In the previous page, the numbers drawn randomly were between 1 and 10. Here, the numbers are between -10 and 10, both -10 and 10 included. Note how negative and postive numbers are displayed using the routine printNum(n,showone). It can either display 1 or not display it depending on the parameter showone. Also, it displays $+3$ instead of 3. Negative numbers are displayed just fine. Here is the code for printNum(n,showone).

function printNum(n, showone) {
 str = ((n > 0) ? "+" : "-");
 str += showone ? Math.abs(n) : ((Math.abs(n) == 1) ? "" : Math.abs(n));
 return str;
 }

Here is the modified code of changeProblem()

function changeProblem() {
 max = 10;min = -10;range = max - min;
 m = Math.floor(range * Math.random()) + min;
 n = Math.floor(range * Math.random()) + min;

 // don't want the x term zero or the constant term zero
 if (m+n == 0) m = n; 
 if (m*n == 0) {m = -2; n = 3};

 s = m + n;
 p = m*n;
 divElement = document.getElementById("equation");
 str = "<p/>$x^2" + printNum(s,false)  + "x" + printNum(p,true) + "$";
 str += "<p/>$=x^2" + printNum(m,false) + "x" + printNum(n,false) + "x" + printNum(p,true) + "$";
 str += "<p/>$=x(x" + printNum(m,true) + ")" + printNum(n,false) + "(x" + printNum(m,true) + ")$";
 str += "<p/>$=(x" + printNum(m,false) + ")(x" + printNum(n,false) + ")$";
 divElement.innerHTML = str;
 MathJax.Hub.Queue(["Typeset",MathJax.Hub,"equation"]);
 }