<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<title>Multiplying Binomials - RJM Programming - January, 2016 - Thanks to https://www.mathsisfun.com/algebra/polynomials-multiplying.html</title>
<link href='//www.rjmprogramming.com.au/PHP/emboss_h1.css' rel='stylesheet' type='text/css'>
<script type='text/javascript'>

var trstr='';
var rownum=1;
var onesuff=["x", "y"];
var onepref=[1, 2];
var twosuff=["x", "y", ""];
var twopref=[3, -4, 5];
var twosup="²";
var anums=[eval(onepref[0] * twopref[0]), eval(onepref[0] * twopref[1]), eval(onepref[0] * twopref[2]), eval(onepref[1] * twopref[0]), eval(onepref[1] * twopref[1]), eval(onepref[1] * twopref[2])];
var asufs=["x" + twosup, "xy","x", "xy", "y" + twosup, "y"];
var bnums=[eval(onepref[0] * twopref[0]), eval((eval(onepref[0] * twopref[1])) + (eval(onepref[1] * twopref[0]))), eval(onepref[0] * twopref[2]), eval(onepref[1] * twopref[1]), eval(onepref[1] * twopref[2])];
var bsufs=["x" + twosup, "xy","x", "y" + twosup, "y"];
var inbinomial='';
var outsanswer='';
var halfanswer='';
var outanswer='';
var score=0;
var goes=0;
var orig=('(' + onepref[0] + onesuff[0] + (' + ' + onepref[1] + onesuff[1]).replace('+ -','- ') + ')(' + twopref[0] + twosuff[0] + (' + ' + twopref[1] + twosuff[1]).replace('+ -','- ') + (' + ' + twopref[2]).replace('+ -','- ') + ')').replace('(1x','(x').replace('(1x','(x').replace('(1y','(y').replace('(1y','(y').replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
var origh='3x<sup>2</sup> - 4xy + 5x + 6xy - 8y<sup>2</sup> + 10y';
var origa='3x<sup>2</sup> + 2xy + 5x - 8y<sup>2</sup> + 10y';

function doanums() {
onepref[0] = eval((Math.floor(Math.random() * 15) + 1) - (Math.floor(Math.random() * 13) + 1));
onepref[1] = eval((Math.floor(Math.random() * 15) + 1) - (Math.floor(Math.random() * 13) + 1));
twopref[0] = eval((Math.floor(Math.random() * 15) + 1) - (Math.floor(Math.random() * 13) + 1));
twopref[1] = eval((Math.floor(Math.random() * 15) + 1) - (Math.floor(Math.random() * 13) + 1));
twopref[2] = eval((Math.floor(Math.random() * 15) + 1) - (Math.floor(Math.random() * 13) + 1));
if (onepref[0] == 0) onepref[0] = Math.floor(Math.random() * 13) + 1;
if (onepref[1] == 0) onepref[1] = Math.floor(Math.random() * 13) + 1;
if (twopref[0] == 0) twopref[0] = Math.floor(Math.random() * 13) + 1;
if (twopref[1] == 0) twopref[1] = Math.floor(Math.random() * 13) + 1;
if (twopref[2] == 0) twopref[2] = Math.floor(Math.random() * 13) + 1;
orig=('(' + onepref[0] + onesuff[0] + (' + ' + onepref[1] + onesuff[1]).replace('+ -','- ') + ')(' + twopref[0] + twosuff[0] + (' + ' + twopref[1] + twosuff[1]).replace('+ -','- ') + (' + ' + twopref[2]).replace('+ -','- ') + ')').replace('(1x','(x').replace('(1x','(x').replace('(1y','(y').replace('(1y','(y').replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
anums=[eval(onepref[0] * twopref[0]), eval(onepref[0] * twopref[1]), eval(onepref[0] * twopref[2]), eval(onepref[1] * twopref[0]), eval(onepref[1] * twopref[1]), eval(onepref[1] * twopref[2])];
asufs=["x" + twosup, "xy","x", "xy", "y" + twosup, "y"];
bnums=[eval(onepref[0] * twopref[0]), eval((eval(onepref[0] * twopref[1])) + (eval(onepref[1] * twopref[0]))), eval(onepref[0] * twopref[2]), eval(onepref[1] * twopref[1]), eval(onepref[1] * twopref[2])];
bsufs=["x" + twosup, "xy","x", "y" + twosup, "y"];
origh=("" + anums[0] + asufs[0] + (' + ' + anums[1] + asufs[1]).replace('+ -','- ') + (' + ' + anums[2] + asufs[2]).replace('+ -','- ') + (' + ' + anums[3] + asufs[3]).replace('+ -','- ') + (' + ' + anums[4] + asufs[4]).replace('+ -','- ') + (' + ' + anums[5] + asufs[5]).replace('+ -','- ')).replace(twosup, "<sup>2</sup>").replace(twosup, "<sup>2</sup>");
origa=("" + bnums[0] + bsufs[0] + (' + ' + bnums[1] + bsufs[1]).replace('+ -','- ') + (' + ' + bnums[2] + bsufs[2]).replace('+ -','- ') + (' + ' + bnums[3] + bsufs[3]).replace('+ -','- ') + (' + ' + bnums[4] + bsufs[4]).replace('+ -','- ')).replace(twosup, "<sup>2</sup>").replace(twosup, "<sup>2</sup>");
setq();
}

function setq() {
inbinomial=orig;
halfanswer=origh.replace("<sup>2</sup>", twosup).replace("<sup>2</sup>", twosup);
ouranswer=origa.replace("<sup>2</sup>", twosup).replace("<sup>2</sup>", twosup);
document.getElementById('question').value=inbinomial;
document.getElementById('xintermediate_working').innerHTML=halfanswer;
document.getElementById('xquestion').innerHTML=ouranswer;
document.getElementById('intermediate_working').value=puttwos(halfanswer);
document.getElementById('answer').value=puttwos(ouranswer);
document.getElementById('intermediate_working').focus();
}

function checkit() {
var ibits='', fbits='';
var wasscore=score;
var scoresuffix=' ... see correct answers below';
goes++;
score+=compare(document.getElementById('intermediate_working').value, document.getElementById('xintermediate_working').innerHTML, 1);
score+=compare(document.getElementById('answer').value, document.getElementById('xquestion').innerHTML, 2);
if (eval(eval(score - wasscore) % 2) == 1) {
scoresuffix=' ... good work ... now just need to reduce the "xy" bits';
document.title='Awesome intermediate working! ... Multiplying Binomials - RJM Programming - January, 2016 - Thanks to https://www.mathsisfun.com/algebra/polynomials-multiplying.html';
if (eval(score - wasscore) == 3) {
scoresuffix=' ... tremendous work';
ibits = '&nbsp;<font color=darkgreen>&#10004;</font>';
fbits = '&nbsp;<font color=darkgreen>&#10004;</font>';
} else {
ibits = '&nbsp;<font color=darkgreen>&#10004;</font>';
fbits = '<br><font size=1>' + document.getElementById('answer').value + '</font>&nbsp;<font size=3 color=red>X</font>';
}
} else if (eval(score - wasscore) == 2) {
if (document.getElementById('intermediate_working').value.toLowerCase().indexOf('x') != -1 || document.getElementById('intermediate_working').value.toLowerCase().indexOf('y') != -1) {
document.title='Good job! ... Correct intermediate working is shown below (that gets you full points) ... Multiplying Binomials - RJM Programming - January, 2016 - Thanks to https://www.mathsisfun.com/algebra/polynomials-multiplying.html';
ibits = '<br><font size=1>' + document.getElementById('intermediate_working').value + '</font>&nbsp;<font size=3 color=red>X</font>';
fbits = '&nbsp;<font color=darkgreen>&#10004;</font>';
} else {
document.title='Awesome job! ... Multiplying Binomials - RJM Programming - January, 2016 - Thanks to https://www.mathsisfun.com/algebra/polynomials-multiplying.html';
fbits = '&nbsp;<font color=darkgreen>&#10004;</font>';
}
scoresuffix=' ... great work (thinking on your feet)';
} else {
fbits = '<br><font size=1>' + document.getElementById('answer').value + '</font>&nbsp;<font size=3 color=red>X</font>';
ibits = '<br><font size=1>' + document.getElementById('intermediate_working').value + '</font>&nbsp;<font size=3 color=red>X</font>';
}
document.getElementById('score').innerHTML='Score: ' + score + ' / Goes: ' + goes + scoresuffix;
document.getElementById('tr' + rownum).innerHTML='<td style="background-color: yellow;">' + orig + '</td><td style="background-color: lightblue;"> = ' + origh + ibits + '</td><td style="background-color: lightgreen;"> = ' + origa + fbits + '</td>';
document.getElementById('mythead').innerHTML+='<tr id="tr' + eval(rownum + 1) + '">' + trstr + '</tr>';
rownum++;
document.getElementById('intermediate_working').value='';
document.getElementById('question').value='';
document.getElementById('answer').value='';
doanums();
}

function appendtwo(bo) {
document.getElementById(bo.id.replace('p','')).value+=bo.value;
}

function puttwos(inc) {
var maxblanks=' ';
var intc=inc.replace("<sup>2</sup>", twosup).replace("<sup>2</sup>", twosup);
var i, outc='';
var onb=intc.split(twosup);
if (onb[0].length != 0) outc=maxblanks.substring(0, onb[0].length);
for (i=1; i<onb.length; i++) {
outc+=twosup;
if (onb[i].length != 0) outc += maxblanks.substring(0, onb[i].length);
}
return outc;
}

function subcompare(xa, xq, ij) {
var oq=xq, ia, im, minuses, jm, modemustfinds=[twosup, "xy", " "], modemustnotfind1=["=", "=", twosup], modemustnotfind2=["=", "=", "xy"];
var oa=xa, spareoa=oa;
while (oa.indexOf(' ') != -1) {
oa=oa.replace(' ','');
}
while (oq.indexOf(' ') != -1) {
oq=oq.replace(' ','');
}
spareoa=oa.replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('+',' ').replace('-',' ').replace('-',' ').replace('-',' ').replace('-',' ').replace('-',' ').replace('-',' ');
//alert("xa=" + xa + " vs xq=" + xq);
var pluses=spareoa.split(' '); //.oa.split('+');
for (jm=0; jm<3; jm++) {
for (ia=0; ia<pluses.length; ia++) {
if (pluses[ia] != '') {
if (pluses[ia].indexOf('-') == -1) {
if ((" " + pluses[ia]).indexOf(modemustfinds[jm]) != -1 && (" " + pluses[ia]).indexOf(modemustnotfind2[jm]) == -1 && (" " + pluses[ia]).indexOf(modemustnotfind2[jm]) == -1) {
//if (oq.indexOf("+" + pluses[ia]) != -1) {
if ((oa + "+").indexOf("+" + pluses[ia] + "+") != -1 || (oa + "+").indexOf("+" + pluses[ia] + "-") != -1) {
//alert(jm + "Was oq=" + oq + " TryIng to get rid of +" + pluses[ia] + " Now oq=" + oq.replace(pluses[ia], ''));
oq=oq.replace("+" + pluses[ia], '');
} else if ((oa + "+").indexOf("-" + pluses[ia] + "+") != -1 || (oa + "+").indexOf("-" + pluses[ia] + "-") != -1) {
//alert(jm + "Was oq=" + oq + " TryIng to get rid of -" + pluses[ia] + " NoW oq=" + oq.replace(pluses[ia], ''));
oq=oq.replace("-" + pluses[ia], '');
} else {
//alert(jm + "Was oq=" + oq + " Trying to get rid of " + pluses[ia] + " Now oq=" + oq.replace(pluses[ia], ''));
oq=oq.replace(pluses[ia], '');
}
}
} else {
minuses=pluses[ia].split('-');
for (im=1; im<minuses.length; im++) {
if (minuses[im] != '') {
if ((" " + minuses[im]).indexOf(modemustfinds[jm]) != -1) {
//alert(jm + "was oq=" + oq + " trying to get rid of -" + minuses[im] + " now oq=" + oq.replace(minuses[im], ''));
oq=oq.replace("-" + minuses[im], '');
}
}
}
}
}
}
}
//alert("xq=" + oq + " vs xq=" + xq);
if (("~" + oq).replace('~-0','~0').replace('~0x' + twosup,'').replace('~0y' + twosup,'').replace('~0xy','').replace('~0yx','').replace('~0y' + twosup,'').replace('~0x','').replace('~0y','').replace('-0x' + twosup,'').replace('-0y' + twosup,'').replace('+0x' + twosup,'').replace('+0y' + twosup,'').replace('-0xy' + twosup,'').replace('-0xy' + twosup,'').replace('+0xy' + twosup,'').replace('+0xy' + twosup,'').replace('-0x','').replace('+0x','').replace('-0y','').replace('+0y','').replace('-0x','').replace('+0x','').replace('-0y','').replace('+0y','').replace('+','').replace('+','').replace('+','').replace('+','').replace('+','').replace('+','').replace('-','').replace('-','').replace('-','').replace('-','').replace('-','').replace('-','').replace('~','') == '') return ij;
if (ij == 1) {
document.title="You answered with " + xa + " but we prefer the intermediate answer of " + xq;
} else {
alert("You answered with " + xa + " but we prefer the answer of " + xq);
}
return 0;
}

function compare(ina, inq, pluss) {
var applesina=ina.toLowerCase().replace('(1x','(x').replace('(1x','(x').replace('(1y','(y').replace('(1y','(y');
applesina= (" " + applesina).replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
applesina= (" " + applesina).replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
applesina= applesina.replace('+1x','+x').replace('+1x','+x').replace('+1y','+y').replace('+1y','+y');
applesina= applesina.replace('+1x','+x').replace('+1x','+x').replace('+1y','+y').replace('+1y','+y');
applesina= applesina.replace('-1x','-x').replace('-1x','-x').replace('-1y','-y').replace('-1y','-y');
applesina= applesina.replace('-1x','-x').replace('-1x','-x').replace('-1y','-y').replace('-1y','-y');
applesina= applesina.replace('(','').replace('(','').replace(')','').replace(')','').replace('(','').replace('(','').replace(')','').replace(')','');
while (applesina.indexOf('=') != -1) {
applesina= applesina.replace('=','');
}
while (applesina.indexOf('*') != -1) {
applesina= applesina.replace('*','');
}
while (applesina.indexOf(' ') != -1) {
applesina= applesina.replace(' ','');
}
applesina= ("~" + applesina).replace('~+','').replace('~','');
while (applesina.indexOf('+-') != -1) {
applesina= applesina.replace('+-','-');
}
while (applesina.indexOf('-+') != -1) {
applesina= applesina.replace('-+','-');
}
while (applesina.indexOf('yx') != -1) {
applesina= applesina.replace('yx','xy');
}
var applesinq=inq.toLowerCase().replace('(1x','(x').replace('(1x','(x').replace('(1y','(y').replace('(1y','(y');
applesinq= (" " + applesinq).replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
applesinq= (" " + applesinq).replace(' 1x',' x').replace(' 1x',' x').replace(' 1y',' y').replace(' 1y',' y');
applesinq= applesinq.replace('+1x','+x').replace('+1x','+x').replace('+1y','+y').replace('+1y','+y');
applesinq= applesinq.replace('+1x','+x').replace('+1x','+x').replace('+1y','+y').replace('+1y','+y');
applesinq= applesinq.replace('-1x','-x').replace('-1x','-x').replace('-1y','-y').replace('-1y','-y');
applesinq= applesinq.replace('-1x','-x').replace('-1x','-x').replace('-1y','-y').replace('-1y','-y');
applesinq= applesinq.replace('(','').replace('(','').replace(')','').replace(')','').replace('(','').replace('(','').replace(')','').replace(')','');
while (applesinq.indexOf('=') != -1) {
applesinq= applesinq.replace('=','');
}
while (applesinq.indexOf('*') != -1) {
applesinq= applesinq.replace('*','');
}
while (applesinq.indexOf(' ') != -1) {
applesinq= applesinq.replace(' ','');
}
applesinq= ("~" + applesinq).replace('~+','').replace('~','');
while (applesinq.indexOf('+-') != -1) {
applesinq= applesinq.replace('+-','-');
}
while (applesinq.indexOf('-+') != -1) {
applesinq= applesinq.replace('-+','-');
}
while (applesinq.indexOf('yx') != -1) {
applesinq= applesinq.replace('yx','xy');
}
return eval(subcompare(applesina, applesinq, pluss));
return 0;
}

</script>

</head>
<body onload='trstr=document.getElementById("tr1").innerHTML; doanums();' style='background-color: pink;'>
<h1 align='center'>Multiplying Binomials</h1>
<h2 align='center'>RJM Programming - January, 2016</h2>
<h3 align='center' id='score'>Score: 0 / Goes: 0</h3>
<h4 align='center'>Thanks to <a target=_blank title='Maths is Fun' href='https://www.mathsisfun.com/algebra/polynomials-multiplying.html'>Maths is Fun</a></h4>
<div align='center'>
<table align='center'>
<thead id='mythead'>
<tr><th>Question</th><th>Intermediate Working (worth 1 point)</th><th>Answer (worth 2 points)</th></tr>
<tr><td></td><td><input onclick='appendtwo(this);' type='button' id='pintermediate_working' value='²'></input></td><td><input onclick='appendtwo(this);' type='button' id='panswer' value='²'></input></td></tr>
<tr id='tr1'><td><input style='width:100%;background-color: lightgray;' type='text' id='question' value=''></input></td><td><input style='background-color: lightblue;width:100%;' type='text' id='intermediate_working' value=''></input></td><td><input style='background-color: lightgreen;width:100%;' type='text' id='answer' value=''></input></td></tr>
</thead>
<tbody id='mytbody'>
<tr><td><div id='xquestion' style='display: none;'></div></td><td><input style='background-color: yellow;' onclick='checkit();' type='button' id='question' value='Check Answer(s)'></input></td><td><div style='display: none;' id='xintermediate_working'></div></td></tr>
</tbody>
</table>
</div>
</body>
</html>