<HTML> <HEAD> </HEAD> <BODY> <SCRIPT TYPE="TEXT/JAVASCRIPT"> var a = 25; a = a + 8 document.write ('25 + 8 = ' + a + '<BR><BR>') a = "25"; a = a + "8" document.write ('"25" + "8" = ' + a + '<BR><BR>') a = "25"; a = a + 8 document.write ('"25" + 8 = ' + a + '<BR><BR>') a = 25; a = a + "8" document.write ('25 + "8" = ' + a + '<BR><BR>') a = "25"; a = parseInt(a) + 8 document.write ('parseInt("25") + "8" = ' + a + '<BR><BR>') a = "25"; a = a * 1 + 8 document.write ('"25" * 1 + 8 = ' + a + '<BR><BR>') a = 25; a = a + "" + 8 document.write ('25 + "" + 8 = ' + a + '<BR><BR>') </SCRIPT> </BODY> </HTML> |
x = parseInt(y,10)
a + b means a plus b
a - b means a minus b
a * b means a times b
a / b means a divided by b
There is one more useful symbol. "a % b" means the remainder if you divide "a" by "b". For example, "14 % 5" is 4 (since 14 = 5 * 2 + 4).
When doing arithmetic, it is sometimes useful to check that these are numbers! parseInt(..) is good for making a number into a real number, but if it isn't a number to start with, it will have the value NaN. This tests a to see if it is a number.
if (isNaN(a)) {...}
If it is, then it does the contents of the if statement.
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Maths functions function calc() { var inp = document.reals.inpt.value var func = document.reals.functn.value var outp = 0 if (func == 'round') {outp = Math.round (inp)} if (func == 'next') {outp = Math.ceil (inp)} if (func == 'last') {outp = Math.floor (inp)} if (func == 'pos') {outp = Math.abs (inp)} if (func == 'random') {outp = Math.random ()} document.reals.outpt.value = outp } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="reals"> <INPUT TYPE=TEXT NAME="inpt" SIZE=10 VALUE="2.4"> <SELECT NAME="functn" ONCHANGE="calc()"> <OPTION>- please select -</OPTION> <OPTION VALUE="round">rounded</OPTION> <OPTION VALUE="next">next integer</OPTION> <OPTION VALUE="last">previous integer</OPTION> <OPTION VALUE="pos">positive</OPTION> <OPTION VALUE="random">random</OPTION> </SELECT> <INPUT TYPE=TEXT NAME="outpt" SIZE=10 DISABLED> </FORM> </BODY> </HTML> |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Reverse string function reverse() { var inp = document.rev.inpt.value var outp = 0 for (i = 0; i <= inp.length; i++) { outp = inp.charAt (i) + outp } document.reals.outpt.value = outp } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="rev"> <INPUT TYPE=TEXT NAME="inpt" SIZE=20 > <INPUT TYPE=BUTTON VALUE="Reverse" ONCLICK="reverse()"><BR> <INPUT TYPE=TEXT NAME="outpt" SIZE=20 DISABLED> </FORM> </BODY> </HTML> |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Output word function outword(word) { lower = word.toLowerCase() upper = word.toUpperCase() normal = upper.substring(0,1) + lower.substring(1,lower.length) document.write ("<BR>" + lower + "..." + upper + "..." + normal) } </SCRIPT> </HEAD> <BODY> <SCRIPT TYPE="TEXT/JAVASCRIPT"> words = "jan/Feb/MAR/ApR/May/juNe" document.write (words) st = 0 fin = words.indexOf("/",st) while (fin != -1) { outp = words.substring(st,fin) outword(outp) st = fin + 1 fin = words.indexOf("/",st) } outp = words.substring(st,words.length) outword(outp) </SCRIPT> </BODY> </HTML> |
To convert hexadecimal to decimal, use parseInt(x,16)
To convert decimal to hexadecimal, use x.toString(16) - be a bit careful about this though. It gives you the length of hexadecimal number implied by the decimal - so decimal 15 gives hexadecimal F. That might be what you want, of course!
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Sum string until you only have one digit function suminp() { var inp = document.forma.inpt.value while (inp.length > 1) { var outp = 0 for (i = 0; i < inp.length; i++) { outp = outp + parseInt(inp.charAt (i)) } inp = outp + "" } document.forma.outpt.value = inp } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="forma"> Input:<INPUT TYPE=TEXT NAME="inpt" SIZE=20 > <BR><BR><INPUT TYPE=BUTTON VALUE="Sum" ONCLICK="suminp()"> <BR><BR>Output:<INPUT TYPE=TEXT NAME="outpt" SIZE=20 DISABLED> </FORM> </BODY> </HTML> |
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // process input function proc() { var num = document.form2.inpt.value document.form2.outpt.value = r2(num) } // Round to 2 decimal places function r2(n) { ans = n * 1000 ans = Math.round(ans /10) + "" while (ans.length < 3) {ans = "0" + ans} len = ans.length ans = ans.substring(0,len-2) + "." + ans.substring(len-2,len) return ans } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="form2"> Input number: <INPUT TYPE=TEXT NAME="inpt" SIZE=20 > <BR><BR> <INPUT TYPE=BUTTON VALUE="Round to 2 dec. pl." ONCLICK="proc()"> <BR><BR> Output: <INPUT TYPE=TEXT NAME="outpt" SIZE=20 DISABLED> </FORM> </BODY> </HTML> |
var word = document.conv.english.value.split (" ")
takes the input from the form 'conv', text box 'english' and splits it into individual words (which are terminated with a space) which it puts into an array called 'word' - quite a hard-working statement! The rest of the code does the Pig Latin conversion using sub-strings, and outputs it to another text box. This won't work if people use a capital letter or any punctuation of course!
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Convert to Pig Latin function convert() { var word = document.conv.english.value.split (" ") outp = "" for (i = 0; i < word.length; i++) { thisword = word [i] outp = outp + thisword.substring (1, thisword.length) + thisword.substring(0,1) + "ay " } document.conv.piglatin.value = outp } } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="conv"> Input:<INPUT TYPE=TEXT NAME="english" SIZE=40> <BR><BR> <INPUT TYPE=BUTTON VALUE="Convert to Pig Latin" ONCLICK="convert()"> <BR><BR> Output:<INPUT TYPE=TEXT NAME="piglatin" SIZE=40 DISABLED> </FORM> </BODY> </HTML> |
There are times when you have a lot of text which is upper case when you want it to be lower case, or possibly mostly lower case with initial capitals. This piece of JavaScript will do that for you. It is similar to the sub-strings code above, but more generalised. Converting to lower case can be done with a single instruction. It takes a little more effort to search through the strings for spaces, so you can make the next letter into a capital.
<HTML> <HEAD> <SCRIPT TYPE="TEXT/JAVASCRIPT"> // Convert Upper to Lower Case function convcase(word) { lower = word.toLowerCase() upper = word.toUpperCase() normal = upper.substring(0,1) + lower.substring(1,lower.length) for (i = 0; i <= normal.length; i++) { if (normal.substring(i,i+1)==" ") { normal = normal.substring (0,i+1) + upper.substring (i+1,i+2) + normal.substring (i+2,normal.length) } } document.conv2.low.value = lower document.conv2.caps.value = normal } </SCRIPT> </HEAD> <BODY> <FORM ACTION="#" NAME="conv2"> Enter text: <INPUT TYPE=TEXT NAME="up" ONKEYUP="convcase(document.conv2.up.value)" SIZE=20> <BR><BR> All lower case: <INPUT TYPE=TEXT NAME="low" SIZE=20> <BR><BR> Capitals: <INPUT TYPE=TEXT NAME="caps" SIZE=20> </FORM> </BODY> </HTML> |
© Jo Edkins 2005