Javascript Filter

Javascript can filter arrays by looping through the elements and using the boolean If to apply the filter.

There is now, however, an easier method, the filter function.

The filter function consists of two parts: a callback function that returns the values of a boolean operation
function filtr(value) {
return value % 2 == 0;
}

and a filter function of the array that calls the callback function.
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].filter(filtr);

Here is an HTML file that utilizes a filter:

<!DOCTYPE html PUBLIC ‘-//W3C//DTD XHTML 1.0 Transitional//EN’ ‘http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd’&gt;
<html xmlns=’http://www.w3.org/1999/xhtml’&gt;
<head profile=’http://gmpg.org/xfn/11′&gt;
<title>Filter</title>
<style type=’text/css’>
body {margin-left:0;margin-right:0;font:normal normal normal 12px Arial; white-space: pre-wrap; padding: 10px; background: #BBBBBB}
a{ text-decoration: }
:link { color: rgb(0, 0, 255) }
:visited {color :rgb(100, 0,100) }
:hover { }
:active { }
#t1,#t2{background:#FFEEBB; font-weight: 800; padding: 5px}
#ta1,#ta2{background:#FFEEBB; font-weight: 800; border: 3px inset; padding: 5px}
#b1,#b2{border: 2px outset #AAAAAA; border-radius: 4px}
</style>
</head>
<body>
<input type=”text” id=”t1″ name=”t1″ value=’Enter a b c d or e’ ondblclick=’document.getElementById(“t1″).value=””;’ />
<input type=”text” id=”t2″ style=”width: 400px” name=”t2″ value=’Enter Doe Johnson Jones Smith or any part of the names’ ondblclick=’document.getElementById(“t2″).value=””;’ /><input type=”button” id=”b1″ name=”b1″ value=”Letters” onclick=’setVar();’> <input type=”button” id=”b2″ name=”b2″ value=”Names” onclick=’setVar2();’><textarea id=”ta1″ name=”ta1″ rows=”15″ cols=”100″ value=”” ></textarea><textarea id=”ta2″ name=”ta2″ rows=”15″ cols=”100″ value=”” ></textarea>

&lt;script type=”text/javascript” src=”data.js”&gt;&lt;/script&gt;
&lt;script type=”text/javascript”&gt;
myArray2 = new Array(); myArray3 = new Array(); myArraystr= new Array();
var let = “”; var str1 = “1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12”;

function setVar() {
let = document.getElementById(“t1”).value;
myArraystr = [“aa”, “ab”, “ba”, “bb”, “ca”, “cb”, “cc”, “da”, “db”, “dc”, “ea”, “eb”].filter(filtr2);
document.getElementById(“ta2”).value = myArraystr;
document.getElementById(“t1”).value = ‘Enter a b c d or e’;
}

function filtr(value) {
return value % 2 == 0;
}

function filtr2(value) {
return value.substr(0,1) == let && (value.substr(1,1) == “a” || value.substr(1,1) == “b”);
}

var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].filter(filtr);
for (var i=0;i <= myArray.length – 1;i++) {
myArray2[i] = Math.sqrt(myArray[i]).toFixed(5) + ” “;
myArray3[i] = Math.log10(myArray[i]).toFixed(5) + ” “;
}

document.getElementById(“ta1”).value = str1 + “\n” + myArray + “\n” + myArray2 + “\n” + myArray3;

&lt;/script&gt;

</body>
</html>

As can be seen, there is a call to a .js file. I will go into this in more detail later.

The myArray filter mentioned above takes an array of members from 1 to twelve and returns an arry containing only the

even numbers. It is then possible to create other arrays based on the filtered array:
var myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].filter(filtr);
for (var i=0;i <= myArray.length – 1;i++) {
myArray2[i] = Math.sqrt(myArray[i]).toFixed(5) + ” “;
myArray3[i] = Math.log10(myArray[i]).toFixed(5) + ” “;
}

In this case, the boolean operation, return value % 2 == 0; is hard coded and both the squareroot and common log of the resulting array are shown.

There is also a string filter that filters an array of letters based on an optional argument:
function setVar() {
let = document.getElementById(“t1”).value;
myArraystr = [“aa”, “ab”, “ba”, “bb”, “ca”, “cb”, “cc”, “da”, “db”, “dc”, “ea”, “eb”].filter(filtr2);
document.getElementById(“ta2”).value = myArraystr;
document.getElementById(“t1”).value = ‘Enter a b c d or e’;
}

function filtr2(value) {
return value.substr(0,1) == let && (value.substr(1,1) == “a” || value.substr(1,1) == “b”);
}

This filter takes the value from an input box as the first letter and either an a or b as the second letter.

I had previously mentioned data.js. For security reasons, javascript is not allowed to open or save files.

An exception is variables and functions placed in a .js file can be manipulated. This can be exploited to create what is in essence a database, similiar to a csv file, consisting of arrays.

Here is the data.js file:

myArraystr2 = new Array();function setVar2() {
let = document.getElementById(“t2”).value;
myArraystr2 = [“John Doe”, “Jane Doe”, “Bob Johnson”, “Ron Johnson”, “Dick Johnson”, “Jim Jones”, “Mary Jones”, “John Jones”, “Bill Smith”, “Jill Smith”, “Alice Smith”, “Bob Smith”].filter(filtr3);
var str = “”;
for (var i=0;i<=myArraystr2.length – 1; i++) {
str += myArraystr2[i] + ” “;
}
document.getElementById(“ta2”).value = str;
document.getElementById(“t2”).value = ‘Enter Doe Johnson Jones or Smith’;
}function filtr3(value) {
return value.indexOf(let) != -1 ;
}

This function filters an array of names and returns the names that that contain the substring taken from an input box.

Here is how file appears:

Click image for larger view

filter

An Offline HTML Keyboard

This is a keyboard that can be played on a computer or mobile device.

Due to the limited screen size, it was not feasible to have an 88 key board. Only two octaves can be accommodated on any line. Therefore, it was suggested to me that it have multiple registers. With three registers the app spans six octaves.

Here is how it appears:

Click image for larger view

keyboard

Here is the code:

<!DOCTYPE html PUBLIC ‘-//W3C//DTD XHTML 1.0 Transitional//EN’ ‘http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd’&gt;
<html xmlns=’http://www.w3.org/1999/xhtml’&gt;
<head profile=’http://gmpg.org/xfn/11′&gt;
<title>Keyboard</title>
<style type=’text/css’>
body {margin-left:0;margin-right:0;font:normal normal normal 15px Arial; white-space: pre-wrap; background: #DDCC77}
a{ text-decoration: }
:link { color: rgb(0, 0, 255) }
:visited {color :rgb(100, 0,100) }
:hover { }
:active { }
#legato{position: absolute; left: 155px; top: 490px; height: 40px; font:normal normal 800 18px Arial }
#tdw1,#tdw2,#tdw3,#tdw4,#tdw5,#tdw6,#tdw7,#tdw8,#tdw9,#tdw10,#tdw11,#tdw12,#tdw13,#tdw14,#tdw15,#tdw16,#tdw17,#tdw18,#tdw19,#tdw20{position: relative;background: white; border: 2px solid #555555; border-radius: 6px; width: 60px; height: 200px; }
#tdb1,#tdb2,#tdb3,#tdb4,#tdb5,#tdb6,#tdb7,#tdb8,#tdb9,#tdb10{position: absolute;background: black; border: 2px solid #555555; border-radius: 6px; width: 30px; height: 150px; left:5.7%; top: 12px }
#tdb2{left: 12%}
#tdb3{left: 24.7%}
#tdb4{left: 31%}
#tdb5{left: 37.45%}
#tdb6{left: 50.3%}
#tdb7{left: 56.75%}
#tdb8{left: 69.5%}
#tdb9{left: 76.0%}
#tdb10{left: 82.4%}
#piano{position: absolute; top: 245px; width:1000px; left: 175px; background: #BB6633; padding: 10px 10px 0 10px}
#blk{position: absolute; top: 245px; width:1000px; left: 175px;  padding: 10px 10px 0 10px}

#tdw1l,#tdw2l,#tdw3l,#tdw4l,#tdw5l,#tdw6l,#tdw7l,#tdw8l,#tdw9l,#tdw10l,#tdw11l,#tdw12l,#tdw13l,#tdw14l,#tdw15l,#tdw16l,#tdw17l,#tdw18l,#tdw19l,#tdw20l{position: relative;background: white; border: 2px solid #555555; border-radius: 6px; width: 60px; height: 200px; }
#tdb1l,#tdb2l,#tdb3l,#tdb4l,#tdb5l,#tdb6l,#tdb7l,#tdb8l,#tdb9l,#tdb10l{position: absolute;background: black; border: 2px solid #555555; border-radius: 6px; width: 30px; height: 150px; left:5.7%; top: 12px }
#tdb2l{left: 12%}
#tdb3l{left: 24.7%}
#tdb4l{left: 31%}
#tdb5l{left: 37.45%}
#tdb6l{left: 50.3%}
#tdb7l{left: 56.75%}
#tdb8l{left: 69.5%}
#tdb9l{left: 76.0%}
#tdb10l{left: 82.4%}
#pianol{position: absolute; top: 16px; width:1000px; left: 90px; background: #BB6633; padding: 10px 10px 0 10px}
#blkl{position: absolute; top: 16px; width:1000px; left: 90px;  padding: 10px 10px 0 10px}

#tdw1r,#tdw2r,#tdw3r,#tdw4r,#tdw5r,#tdw6r,#tdw7r,#tdw8r,#tdw9r,#tdw10r,#tdw11r,#tdw12r,#tdw13r,#tdw14r,#tdw15r,#tdw16r,#tdw17r,#tdw18r,#tdw19r,#tdw20r{position: relative;background: white; border: 2px solid #555555; border-radius: 6px; width: 60px; height: 200px; }
#tdb1r,#tdb2r,#tdb3r,#tdb4r,#tdb5r,#tdb6r,#tdb7r,#tdb8r,#tdb9r,#tdb10r{position: absolute;background: black; border: 2px solid #555555; border-radius: 6px; width: 30px; height: 150px; left:5.7%; top: 12px }
#tdb2r{left: 12%}
#tdb3r{left: 24.7%}
#tdb4r{left: 31%}
#tdb5r{left: 37.45%}
#tdb6r{left: 50.3%}
#tdb7r{left: 56.75%}
#tdb8r{left: 69.5%}
#tdb9r{left: 76.0%}
#tdb10r{left: 82.4%}
#pianor{position: absolute; top: 475px; width:1000px; left: 260px; background: #BB6633; padding: 10px 10px 0 10px}
#blkr{position: absolute; top: 475px; width:1000px; left: 260px;  padding: 10px 10px 0 10px}

</style>


var old = “tdw1l”; var chrd = false; var legato = false;

function embed(layr,note) {
if (legato) {
chrd = true;
} else {
chrd = false;
}
if (! chrd) document.getElementById(old).innerHTML = ”;
document.getElementById(layr).innerHTML = ‘   ‘;
old = layr;
}

function makeKeyboard() {
var str = “

“;
for (var i=1; i
str += ‘

‘;
}
document.getElementById(“piano”).innerHTML = str + “

“;
str = “”;

for (var i=1; i
str += ‘

‘;
}
document.getElementById(“blk”).innerHTML = str
str = “

“;

for (var i=1; i
str +=’

‘;
}
document.getElementById(“pianol”).innerHTML = str + “

“;
str = “”;

for (var i=1; i
str += ‘

‘;
}
document.getElementById(“blkl”).innerHTML = str;

str = “

“;

for (var i=1; i
str +=’

‘;
}
document.getElementById(“pianor”).innerHTML = str + “


str = “”;

for (var i=1; i
str += ‘

‘;
}
document.getElementById(“blkr”).innerHTML = str;

}

function playChord(note1, note2, note3 ) {
chrd = true; legato = true;
embed(note1, note1 + “.mp3”);
embed(note2, note2 + “.mp3”);
embed(note3, note3 + “.mp3”);
chrd = false;
}

function leg() {
if (legato) {
legato = false;
document.getElementById(“legato”).value=”Pedal Off”;
} else {
legato = true;
document.getElementById(“legato”).value=”Pedal On”;
}
}

</head>
<body onload = ‘makeKeyboard();’>
<table id=”piano”></table>


<table id=”pianol”></table>


<table id=”pianor”></table>


<input type=”button” id=”legato” name=”legato” value=”Pedal Off” onclick=’leg();’ />
</body>
</html>

The registers are set up as tables, with the cells as keys. To save on text, the keys are inserted programatically through the function makeKeyboard().
var str = “<tr>”;
for (var i=1; i <= 15; i ++) {
str += ‘<td id=”tdw’ + i + ‘” onclick=\’embed(“tdw’ + i + ‘”, “tdw’ + i + ‘.mp3”);\’ ></td>’;
}
document.getElementById(“piano”).innerHTML = str + “</tr>”;
str = “”;

str += ‘

‘;
}
document.getElementById(“blk”).innerHTML = str

Notes are played with the function embed():
function embed(layr,note) {
if (legato) {
chrd = true;
} else {
chrd = false;
}
if (! chrd) document.getElementById(old).innerHTML = ”;
document.getElementById(layr).innerHTML = ‘<audio  autoplay> <source src=’ + note + ‘ />  </audio>’;
old = layr;
}

The note fonts have a duration of two seconds. By default, hitting a new note stops the previous note automatically. If a pedal effect is desired, a butoton can toggle a pedal on and off.

The variable old has the name of the previous key. If the pedal is off, The innerHTML of the previous is set to a blank string, immedialely shutting it off. Else it plays for the full two seconeds.
if (! chrd) document.getElementById(old).innerHTML = ”;

If you want to try the keyboard, you can download it Here. Click on Download and download piano.zip.

An HTML Exponential Notation Converter

Javascript has its own function to convert a number to exponential form, called toExponent(). However, in my opinion, the format is terrible, not the least in the number of decimal places carried in the printout.

Here is how it looks:

Click image for larger view

js-exponential

I think you would agree it is hard to see the number through all the digits after the period.

I therefor set out to create my own function (toExpon()), limiting the number of fixed places in the decimal.

Here is the code:

<!DOCTYPE html PUBLIC ‘-//W3C//DTD XHTML 1.0 Transitional//EN’ ‘http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd’&gt;
<html xmlns=’http://www.w3.org/1999/xhtml’&gt;
<head profile=’http://gmpg.org/xfn/11′&gt;
<title>Exponential Converter</title>
<style type=’text/css’>
body {margin-left:0;margin-right:0;font:normal normal normal 12px Arial; white-space: pre-wrap}
a{ text-decoration: }
:link { color: rgb(0, 0, 255) }
:visited {color :rgb(100, 0,100) }
:hover { }
:active { }
.frame{ top: 5%; margin: 20px; background: #EEEEEE; border: 6px outset; width: 50%}
input{position: relative; width: 50%; border: 4px inset}
button{border: 4px outset}
</style>function toExpon() {
var val1 = document.getElementById(“from”).value;
val1 = calc(val1).toExponential();
if(! isNaN(val1)) no = val1.toString();var a = no.indexOf(“e”);
var xp = no.substr(a, no.length – a);
if (xp.indexOf(“+”) > -1) xp = xp.substr(0,1) + xp.substr(2,xp.length – 2);
no = parseFloat(no.substr(0, a)).toFixed(4).toString() + ” ” + xp;
document.getElementById(“sci”).value = no;
}

function calc(exp) {
var lft = 0;
var rgt = 0;
var a = -1;
var ab = -1;
var tmp1 = “”;
var tmp2 = “”;
var tmp3 = “”;

if(exp.indexOf(“(“) > -1){
a = exp.indexOf(“(“);
ab = exp.indexOf(“)”, a + 1);
tmp1 = exp.substr(0, a-1);
if(exp.indexOf(“*”) > -1) tmp3 = ” *”;
exp = exp.substr(a+1,ab-(a+1));
tmp2 = “(“;
}

if(exp.indexOf(“+”) > -1) a = exp.indexOf(“+”);
if(exp.indexOf(“*”) > -1) a = exp.indexOf(“*”);
if(exp.indexOf(“log”) > -1) a = exp.indexOf(“log”) + 2;
if(a > -1) {
if (exp.indexOf(“*”) > -1 || exp.indexOf(“+”) > -1) {
if (lft == 0) lft = parseFloat(exp.substr(0,a-1));
if (rgt == 0)rgt = parseFloat(exp.substr(a + 1,exp.length – a));
} else if(exp.indexOf(“log”) > -1) {
lft = parseFloat(exp.substr(a+1, exp.length – a));
}
} else {
lft = exp;
}

if (exp.indexOf(“+”) > -1) {
var result = lft + rgt;

if(tmp2 == (“(“)) {
rgt = result;
lft = parseFloat(tmp1);
exp = ” *”;
}
}
if (exp.indexOf(“*” ) > -1) {
result = lft * rgt;
} else if (exp.indexOf(“log”) > -1) {
result = Math.log10(lft);
} else {
result = parseFloat(lft);
}

return result;
}

</head>
<body>

Convert

</body>
</html>

The html creates the interface

Click image for larger view
sciconv

There are four conditions that have to be considered; A integer with an absolute value larger than 1, a floating point number with an absolute value larger than 1, A floating point number with an absolute value between 0 and 1 and a very larger or very small number.

At first I started from scratch and I had something working, but I realized that since javascript automatically converted very large and small number to exponential form in a horrible format, it was easiest  to convert all numbers the the exponential form and then convert one form only.

Here is how a conversion would look:

Click image for larger version.

sciconvcalc

much tidier than with the innate function,;

As a bonus, I added another function to do some simple calculation. It can express an addition, multiplication or the common logarithm of a number in exponential notation.

It is called calc() and extracts the numbers from the expressions to perform the the calculation.

An Offline Complex Number Calculator

Complex numbers are ways to express numbers in two dimensional space. For example, the numbers 1, 2, -1, 0, etc. can all be plotted on a horizontal line. But what if a point is above the line? Then both horizontal and vertical values are needed. In a complex formula, this is expressed as ax + byi, where i is the square root of -1. The first half of a complex number is called the real value while the second is called the imaginary value. Complex math is different from math with real numbers only.

For instance, the square of x is x2. the square of x + yi would be x2 + 2xyi + y2i2. Since i2 is -1, that resolves to x2 + 2xyi -y2. The real half would then be x2 – y2 while the imaginary half would be 2xyi.

This calculator performs a few calculations on complex numbers. It can also be used as a regular calculator by omitting the imaginary half. It is hardly complete and will be updated periodically in the future.

The app consists of an html and a js file. I will not show the complete code, but you can download a zip file here. Click on Downloads and download ccalc.zip.

Here is how the interface appears:

Click image for larger view

ccalc-interface
The Two halves are entered real half first, followed by a space and the imaginary half minus the i. With the exception of addition, subtraction, multiplication and division, which require the value of the second input box to be performed on the first, all operations can be performed on both inputs, giving two results. All parameters can take either pi or e as an argument.

There are two drop down menus with operations. The one on the left raises the number to a constant power. Two to six can be selected directly. If a number is placed in the “value of constant” box, clicking n raises the number to that power.

Here is how that menu appears:

Click image for larger view

powers

Here are the other functions currently available:

Click image for larger view

otherfunct

As I said, the calculator will be expanded in future versions.

The html file sets up the interface and call the js file on changing the value of one of the select
boxes:
<select id=”s1″ name=”s1″ onchange=’obj();’ >

Double clicking a text box empties it to allow for quick new data entry:
<input type=’text’ id=’t2′ name=’t2′ value=’1st number eg: 1.5 4′ ondblclick=’document.getElementById(“t2”).value = “”‘/>

Here is how variables are set up and the operator functions called:
function obj() {
x = document.getElementById(“t2”).value;
var a = x.indexOf(” “);
if (a != -1) {
x1 = x.substr(0,a);
y1 = x.substr(a,x.length);
} else {
x1 = x;
y1 = “0”;
}
y = document.getElementById(“t3”).value;
var b = y.indexOf(” “);
if (b != -1) {
x2 = y.substr(0,b);
y2 = y.substr(b,y.length);
} else {
x2 = y;
y2 = “0”;
}

if (document.getElementById(“t3”).value == “2nd number eg: 1.5 4” || document.getElementById(“t3”).value == “” ) {
x2 = “1”;
y2 = “0”;
}

if (x1 == “pi”) x1 = Math.PI;
if (x1 == “e”) x1 = Math.E;
if (x2 == “pi”) x2 = Math.PI;
if (x2 == “e”) x2 = Math.E;
if (y1.indexOf(“e”) > -1) {
y1 = Math.E;
} else if (y1.indexOf(“pi”) > -1) {
y1 = Math.PI;
}
if (y2.indexOf(“e”) > -1) {
y2 = Math.E;
} else if (y2.indexOf(“pi”) > -1) {
y2 = Math.PI;
}

no = document.getElementById(“s1”).value;
no2 = document.getElementById(“s2”).value;

if (no == 2) {
f1.zx = sqr().zx;
f1.zy = sqr().zy;
f1.zx2 = sqr().zx2;
f1.zy2 = sqr().zy2;

if (no2 == “Sum”) {
f1.zx = parseFloat(x1) + parseFloat(x2);
f1.zy = parseFloat(y1) + parseFloat(y2);

Here is a typical operator function:

function mult() {
zx = (x1 * x2) – (y1 * y2);
zy = (x1 * y2) + (x2 * y1);
zx2 = “”;
zy2 = “”;
return {zx,zy,zx2,zy2};
}

This calculator uses objects:
var f1={zx:0,zy:0, zx2:0,zy2:0};

This is a way to return more than one value from a function.

Here is how the output is formatted.

if(Math.abs(f1.zx < .01)) powr1 = true;
if(Math.abs(f1.zy < .01)) powr2 = true;
if(Math.abs(f1.zx2 < .01)) powr3 = true;
if(Math.abs(f1.zy2 < .01)) powr4 = true;

var v = Math.round(f1.zx) – f1.zx;
if (Math.abs(v) < .001 && ! powr1) f1.zx = Math.round(f1.zx);
v = Math.round(f1.zy) – f1.zy;
if (Math.abs(v) < .001 && ! powr2) f1.zy = Math.round(f1.zy);
v = Math.round(f1.zx2) – f1.zx2;
if (Math.abs(v) < .001 && ! powr3) f1.zx2 = Math.round(f1.zx2);
v = Math.round(f1.zy2) – f1.zy2;
if (Math.abs(v) < .001 && ! powr4) f1.zy2 = Math.round(f1.zy2);
if (f1.zx > Math.floor(f1.zx)) {
num1 = f1.zx.toFixed(4);
} else {
num1 = Math.floor(f1.zx);
if (num1 == 0) num1 = “”;
}

if (f1.zy > Math.floor(f1.zy)) {
num2 = ” + ” + f1.zy.toFixed(4).toString() + “i”;
} else {
num2 = ” + ” + Math.floor(f1.zy).toString() + “i”;
if (num2.indexOf(“0”) > -1) num2 = “”;
}
if (num1 == “”) num2 = num2.substr(2, num2.length);

if (f1.zx2 > Math.floor(f1.zx2)) {
num3 = f1.zx2.toFixed(4);
} else {
num3 = Math.floor(f1.zx2);
}

if (f1.zy2 > Math.floor(f1.zy2)) {
num4 = ” + ” + f1.zy2.toFixed(4).toString() + “i”;
} else {
num4 = ” + ” + Math.floor(f1.zy2).toString() + “i”;
if (num4.indexOf(“0”) > -1) num4 = “”;
}

if (powr1) {
for (var i=1;i<30;i++) {
var ab = f1.zx.toString().indexOf(“0”,ab + 1);
if (ab == -1) {
num1 = f1.zx.toFixed(i + 3);
break;
}
}
}

if (powr2) {
for (var i=1;i<30;i++) {
var ab = f1.zy.toString().indexOf(“0”,ab + 1);
if (ab == -1) {
num2 = ” + ” + f1.zy.toFixed(i + 3).toString() + “i”;
if (num1 == “”) num2 = num2.substr(2, num2.length);
break;
}
}
}

if (powr3) {
for (var i=1;i<30;i++) {
var ab = f1.zx2.toString().indexOf(“0”,ab + 1);
if (ab == -1) {
num3 = f1.zx2.toFixed(i + 3);
break;
}
}
}

if (powr4) {
for (var i=1;i<30;i++) {
var ab = f1.zy2.toString().indexOf(“0”,ab + 1);
if (ab == -1) {
num4 = ” + ” + f1.zy2.toFixed(i + 3).toString() + “i”;
if (num3 == “”) num4 = num4.substr(2, num4.length);
break;
}
}
}

if (! isNaN(num1) || ! isNaN(num2) ) {
document.getElementById(“t1”).value=num1 + num2;
} else {
document.getElementById(“t1”).value = “”;
}

if (! isNaN(num3) || ! isNaN(num4) ) {
document.getElementById(“t4”).value=num3 + num4;
} else {
document.getElementById(“t4”).value = “”;
}
if (document.getElementById(“t3”).value == “2nd number eg: 1.5 4” || document.getElementById(“t3”).value == “”) document.getElementById(“t4”).value = “”;

if (no2 == “Multiply” || no2 == “Divide” || no2 == “Sum” || no2 == “Difference”) document.getElementById(“t4”).value= “”;
if(document.getElementById(“s2”).value != “Other Function”) document.getElementById(“s2”).value = “Other Function”;
document.getElementById(“s1″).value=”Power”;
powr1 = false;
powr2 = false;
powr3 = false;
powr4 = false;
}

If a number is less than .01, it is given special treatment:
if(f1.zx < .01) powr1 = true;
if (powr1) {
for (var i=1;i<30;i++) {
var ab = f1.zx.toString().indexOf(“0”,ab + 1);
if (ab == -1) {
num1 = f1.zx.toFixed(i + 3);
break;
}
}
}

Otherwise it is either rounded to an integer or a float value rounded to 4 significant places:
var v = Math.round(f1.zx) – f1.zx;
if (Math.abs(v) < .001 && ! powr1) f1.zx = Math.round(f1.zx);
v = Math.round(f1.zy) – f1.zy;
if (Math.abs(v) < .001 && ! powr2) f1.zy = Math.round(f1.zy);
v = Math.round(f1.zx2) – f1.zx2;
if (Math.abs(v) < .001 && ! powr3) f1.zx2 = Math.round(f1.zx2);
v = Math.round(f1.zy2) – f1.zy2;
if (Math.abs(v) < .001 && ! powr4) f1.zy2 = Math.round(f1.zy2);
if (f1.zx > Math.floor(f1.zx)) {
num1 = f1.zx.toFixed(4);
} else {
num1 = Math.floor(f1.zx);
if (num1 == 0) num1 = “”;
}

The value is then outputted:
if (! isNaN(num1) || ! isNaN(num2) ) {
document.getElementById(“t1”).value=num1 + num2;
} else {
document.getElementById(“t1”).value = “”;
}

Here is how it looks after a calculation:

Click image for larger view

calculation

There is also a memory to allow for multiple calculations:
function m(numb) {
if (numb == 2) {
mx[numb] = f1.zx;
my[numb] = f1.zy;
} else if (numb == 3) {
mx[numb] = f1.zx2;
my[numb] = f1.zy2;
}
}

function mr(numb) {
document.getElementById(“t” + numb).value=mx[numb] + ” ” + my[numb];
}

Clicking M1 or M2 and then MR1 or MR2 places the first or second result back as an input for further calculations. These functions insert the full values, not the rounded ones seen in the output.

Here is how that looks:

click image for larger view

memory