1 <HTML> 2 <HEAD> 3 <TITLE>JavaScript Example: for-loop</TITLE> 4 </HEAD> 5 6 <BODY BGCOLOR="#FFFFFF"> 7 8 <PRE> 9 <SCRIPT LANGUAGE="JavaScript"> 10 <!-- hide script from old browsers 11 12 // Compute x^k where k is an integer: 13 function ipow(x, k) { 14 var y = 1; 15 for ( var i = 0; i < Math.abs(k); i++ ) { 16 y *= x; 17 } 18 if ( k < 0 ) return 1/y; else return y; 19 } 20 21 document.writeln("2^3 = " + ipow(2, 3)); 22 document.writeln("2^(-3) = " + ipow(2, -3)); 23 document.writeln("10^0 = " + ipow(10, 0)); 24 document.writeln("0^10 = " + ipow(0, 10)); 25 document.writeln("0^0 = " + ipow(0, 0)); 26 document.writeln("1.1^10 = " + ipow(1.1, 10)); 27 document.writeln("PI^2 = " + ipow(Math.PI, 2)); 28 29 // end script hiding --> 30 </SCRIPT> 31 </PRE> 32 33 </BODY> 34 </HTML>