diff options
Diffstat (limited to 'content/js/math.js')
-rwxr-xr-x | content/js/math.js | 274 |
1 files changed, 274 insertions, 0 deletions
diff --git a/content/js/math.js b/content/js/math.js new file mode 100755 index 0000000..a0b9da4 --- /dev/null +++ b/content/js/math.js @@ -0,0 +1,274 @@ +var ctx, xs, ys, columnWidth, rowHeight, solution, steps, typeFlag; +var decimalNumber,words,ask,txtComment,spaces; +var digits, WBDigits, WBTeen, WBTens; +var whole, decimal,wholeDigits,dcmlDigits; +var caseno, cases; + +txtComment = ''; +spaces = ' '; +WBDigits=['zero','one','two','three','four','five','six','seven','eight','nine']; +WBTeens=['ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen']; +WBTens=['','','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety']; + +function word(s){ + n = parseInt(s); + if (n<10){ + return WBDigits[n] + } else if (n<20) { + return WBTeens[n-10] + } else { + x = Math.floor(n/10); + y = n%10; + if (y>1){ + return WBTens[x] + '-' + WBDigits[y]; + } else { + return WBTens[x]; + }; + }; +}; + +function getUnits(theDigits){ + s = ""; + units = theDigits.length+""; + switch(units){ + case "1": + if(theDigits[0] != '0'){ + s = word(theDigits[0]); + }; + break; + case "2": + indx = theDigits[1]*10+parseInt(theDigits[0]); + s = word(indx); + break; + case "3": + s = s + word(theDigits[2]) + ' hundred '; + indx = theDigits[1]*10+parseInt(theDigits[0]); + if(indx>0){ + s = s + word(indx); + }; + break; + }; + return s; +}; + +function getDecimals(theDigits){ + s = ""; + decimals = theDigits.length+""; + switch(decimals){ + case "1": + s = word(theDigits[0]) + ' tenths'; + break; + case "2": + indx = theDigits[1]*10+parseInt(theDigits[0]); + s = word(indx) + ' hundredths'; + break; + case "3": + if(parseInt(theDigits[2])>0){ + s = word(theDigits[2]) + ' hundred '; + }; + indx = theDigits[1]*10+parseInt(theDigits[0]); + s = s + word(indx) + ' thousandths'; + break; + default: + alert(decimals+' should not happen'); + }; + return s; +}; + +function getWords(theDigits){ + test=theDigits.join(""); + if (test.indexOf('.')>0){ + parts = test.split('.'); + decimalDigits=parts[0].split(""); + unitDigits=parts[1].split(""); + }else{ + decimalDigits = []; + unitDigits=test.split(""); + }; + decimals = decimalDigits.length; + units = unitDigits.length; + if(units>0){ + words = getUnits(unitDigits); + }else{ + words = ''; + }; + if(decimals>0){ + dwords = getDecimals(decimalDigits); + if(dwords.length>0){ + if(words.length>0){ + words = words + ' and ' + dwords; + }else{ + words = dwords; + }; + }; + }; + return words +}; + +function getWholeWords(words){ + pos = words.indexOf(' and '); + if(pos>0){ + txt = words.substring(0,pos); + return txt; + }else{ + return ""; + } +}; + +function getRandom(a,b) { + c = Math.random() + return Math.floor(c * (b-a) + a) +}; + +function generate_compare(op1,op2){ + if (op1 == op2){ + return '='; + } else if (op1 < op2){ + return '<'; + } else { + return '>'; + }; +}; + +function expandNumber(number){ + position = 0; + rest = number; + digits = []; + while (rest > 0) { + digit = rest%10; + digits[position] = digit; + position += 1; + rest = (rest - digit) / 10; + }; + return digits; +}; + +function drawDigits(row, xunits, theDigits){ + y = ys + (row+2)*50; + xl = xunits - 20; + for(i=0;i<theDigits.length;i++){ + x = xl-(i*columnWidth); + ctx.fillText(theDigits[i],x,y-10); + ctx.stroke(); + }; +}; + +function drawHighlight(row,x,number,color){ + y = ys + (row+2)*50; + save = ctx.fillStyle; + ctx.fillStyle = color; + ctx.fillText(number,x,y-10); + ctx.stroke(); + ctx.fillStyle = save; +}; + +function drawBlanks(row,col,cols){ + y = ys + (row+2)*50; + x = xs + 50 + col*columnWidth; + ctx.clearRect(x,y-50,x+50-col*columnWidth,y); +}; + +function drawNumber(row, x, number){ + y = ys + (row+2)*50; + ctx.fillText(number,x,y-10); + ctx.stroke() +}; + +function drawLine(xs,ys,xf,yf){ + ctx.moveTo(xs,ys) + ctx.lineTo(xf,yf) + ctx.stroke() +}; + +function drawTotal(row, xunits, sub, cols){ + //draw heavier line at base of row for cols columns from right + //if sub is true, draw not so bold a line + y = ys + (row+2)*50; + xl = xunits; + drawLine(xl - cols * columnWidth, y, xl, y); +}; + +function drawComment(row, xunits, txt){ + if(row==0){ + y = ys; + x = xs; + align=ctx.textAlign; + ctx.textAlign='left'; + ctx.fillText(spaces,x,y-10); + ctx.stroke(); + }else{ + y = ys + (row+2)*50; + x = xunits + 25; + align = ctx.textAlign; + }; + ctx.fillText(txt,x,y-10); + ctx.stroke(); + ctx.textAlign = align; +}; + +function drawPanel(name){ + // draw lines + xf = xs+3*columnWidth + yf = ys+rows*rowHeight + first = xs+columnWidth + second = xs+2*columnWidth + // horizontal + drawLine(xs,ys,xf,ys); + drawLine(xs,ys+rowHeight,xf,2*rowHeight); + drawLine(xs,ys+2*rowHeight,xf,3*rowHeight); + //drawLine(xs,yf,xf,yf); + // vertical + drawLine(xs,ys,xs,yf); + drawLine(xf,ys,xf,yf); + drawLine(first,ys+rowHeight,first,yf); + drawLine(second,ys+rowHeight,second,yf); + // text + font = ctx.font; + align = ctx.textAlign; + ctx.font = "12pt Arial"; + ctx.textAlign = "left"; + ctx.fillText(name[0], first+10, ys+40); + ctx.stroke(); + ctx.fillText(name[1],xs+10, ys+90); + ctx.stroke(); + ctx.fillText(name[2], first+10, ys+90); + ctx.stroke(); + ctx.fillText(name[3], second+10, ys+90); + ctx.stroke(); + ctx.font = font; + ctx.textAlign = align; +}; + +//draw place value table +function draw(){ + // set up canvas + var canvas = document.getElementById('placeValue'); + if (canvas.getContext){ + ctx = canvas.getContext('2d'); + var maxx = 1198; + var maxy = 700; + canvas.setAttribute('width', maxx); + canvas.setAttribute('height', maxy); + ctx.fillStyle = 'black'; + ctx.strokeStyle = 'black'; + ctx.font = "20pt Arial"; + ctx.textAlign = "center"; + // draw panels + columnWidth = 100; + rowHeight = 50; + rows = 7; + xs = 50 + 6*columnWidth; + ys = 50 + decimals = ['thousandths','10ths','100ths','1000ths'] + drawPanel(decimals); + xs = 50 + 3*columnWidth; + units = ['units','hundreds','tens','ones'] + drawPanel(units); + xs = 50; + thousands = ['thousands','hundreds','tens','ones'] + drawPanel(thousands); + drawComment(1,30+6*columnWidth,'.') + } else { + alert('oops'); + }; +}; |