$x^2 - 3x + 2$

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

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

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

Note that previously it was displaying $1x$ or $1(x - 2)$, but it is now displaying $x$ and $(x - 2)$. It is not displaying the coefficient if the coefficient is 1.

Here is the modified code.

function changeProblem() {
 m = Math.floor(10 * Math.random()) + 1;
 n = Math.floor(10 * Math.random()) + 1;
 s = m + n;
 p = m*n;
 divElement = document.getElementById("equation");
 str = "<p/>$x^2" + "-" + s  + "x" + "+" + p + "$";
 str += "<p/>$=x^2" + "-" + ((m == 1) ? "" : m) + "x" + "-" + ((n == 1) ? "" : n) + "x" + "+" + p + "$";
 str += "<p/>$=x(x- " + m + ")-" + ((n == 1) ? "" : n) + "(x -" + m + ")$";
 str += "<p/>$=(x" + "-" + m + ")(x" + "-" + n + ")$";
 divElement.innerHTML = str;
 MathJax.Hub.Queue(["Typeset",MathJax.Hub,"equation"]);
 }