使用prompt输入一个正整数n,以表格的形式在网页上输出1-n的平方和立方。如输入5,显示

n

1

2

3

4

5

n2

1

4

9

16

25

n3

1

8

27

64

125

 

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4.   <title>test3_1</title> 
  5.   <script type="text/javascript" src="out_put.js"> 
  6.   function input()  
  7.     { 
  8.         do 
  9.         { 
  10.             n = prompt("请输入n的值!",""); 
  11.             if(n == null)     
  12.             { 
  13.                 return false;   
  14.             }    
  15.             if(n.replace(/^\s+|\s+$/g,"") == "")     
  16.             {  
  17.                 alert("输入内容为空!");  
  18.             } 
  19.         } 
  20.         while (n.replace(/^\s+|\s+$/g,"") == "" && n != null); 
  21.     } 
  22.  
  23.     input();  
  24.   
  25.     var m; //定义行  
  26.   
  27.     setrc(3,n);  //调用指定行列输出表格,根据内容数组维来指定几行几列  
  28.   
  29.     function setrc(m,n) 
  30.     {   //指定行列输出表格函数  
  31.         if(n.replace(/^\s+|\s+$/g,"") != "" && n != null) 
  32.         { 
  33.             document.write("<table border=2 bgcolor=green align=center cellpadding=10width=500 >")  
  34.             for(i=1;i<=m;i++)  
  35.             { 
  36.                 document.write("<tr align=center >") 
  37.                 if(i==1) 
  38.                     {document.write("<td>n")} 
  39.                 if(i==2) 
  40.                     {document.write("<td>n<sup>2</sup>")} 
  41.                 if(i==3) 
  42.                     {document.write("<td>n<sup>3</sup>")} 
  43.                 for(j=1;j<=n;j++)  
  44.                     document.write("<td>"+Math.pow(j,i))  //输出数组  
  45.               
  46.             }  
  47.             document.write("</table>")  
  48.         } 
  49.         else 
  50.             return false; 
  51.     } 
  52.   </script> 
  53. </head> 
  54.     <body> 
  55.  
  56.     </body> 
  57. </html> 

 

 
  

效果如下: