1.判断闰年
var i;
for(i=1980;i<=2016;i++)
{
if( i % 4 == 0 && i % 100 != 0 || i % 400 == 0)
document.write( i + " ");
}
2.九九乘法表
var i, j;
for(i=0;i<10;i++)
{
for(j=1;j<=i;j++){
document.write( i + "*"+ j +"=" +i*j );
document.write(" ");
}
document.write("<br/>");
}
3.100-999的水仙花:
var i,a = 0,b =0,c = 0;
for(i = 100;i < 1000;i++){
a = parseInt(i / 100);
b = parseInt(i / 10 - a * 10);//b = parseInt(i /%100 /10);
c = parseInt(i % 10);
if( i==(a * a * a + b * b * b + c * c * c))
document.write( i + " ");
}
第二种方法:
for(var i = 1;i < 10;i++){
for(var j = 0;j < 10;j++){
for(var k=0; k < 10;k++){
if(i * 100 + j * 10 + k == i * i * i + j * j * j + k * k * k){
document.write(i * 100 + j * 10 + k+" ");
}
}
}
}
4. 1+3+5+7.......+99
var i,sum=0;
document.write("1+3+5+7.......+99的结果为:");
for(i = 0;i < 100;i++){
if(i % 2 != 0 ){
sum+=i;
}
}
document.write(sum+ " ");
5.100以内的阶乘
var i,sum=1;
document.write("100以内的阶乘结果为:");
for(i = 1;i <= 100;i++){
sum*=i;
}
document.write(sum+ " ");
6.打印三角形
for( var i = 0; i < 6; i++){
for( var j = 0; j < 5 - i ;j++ ){
document.write(" ");
}
for( var j = 0; j < i ;j++ ){
document.write("*");
}
document.write("<br/>");
}
7.菱形
for( var i = 0; i < 6; i++){
for( var j = 0; j < 5 - i ;j++ ){
document.write(" ");
}
for( var j = 0; j < 2*i+1 ;j++ ){
document.write("*");
}
document.write("<br/>");
}
for( var i = 0; i < 5; i++){ for( var j = 0; j < i+1;j++ ){ document.write(" "); } for( var j = 0; j < 2*(4-i)-1 ;j++ ){ document.write("*"); }document.write("<br/>");}
8.空菱形
for( var i = 0; i < 6; i++){
for( var j = 0; j < 5 - i ;j++ ){
document.write(" ");
}
for( var j = 0; j < 2 * i + 1 ;j++ ){
if(j == 0 || j == 2 * i)
document.write("*");
else{
document.write(" ");
}
}
document.write("<br/>");
}
for( var i = 0; i < 5; i++){ for( var j = 0; j < i + 1;j++ ){ document.write(" "); } for( var j = 0; j < 2 * (4 - i)-1 ;j++ ){ if(j == 0 || j == 2 * (4 - i)-2) document.write("*"); else{ document.write(" "); } }document.write("<br/>");}
9.判断今天是星期几
var now = new Date();
var day = now.getDay();
var str = " ";
switch(day){
<span style="white-space:pre"></span>case 1:
<span style="white-space:pre"></span>str = "星期一";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 2:
<span style="white-space:pre"></span>str = "星期二";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 3:
<span style="white-space:pre"></span>str = "星期三";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 4:
<span style="white-space:pre"></span>str = "星期四";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 5:
<span style="white-space:pre"></span>str = "星期五";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 6:
<span style="white-space:pre"></span>str = "星期六";
<span style="white-space:pre"></span>break;
<span style="white-space:pre"></span>case 7:
<span style="white-space:pre"></span>str = "星期天";
<span style="white-space:pre"></span>break;
}
document.write("今天是:" + str);
10.空三角
for( var i = 0; i < 6; i++){
for( var j = 0; j < 5 - i ;j++ ){
document.write(" ");
}
for( var j = 0; j < 2 * i + 1 ;j++ ){
if(j == 0 || j == 2 * i)
document.write("*");
<span style="white-space:pre"></span>else{
<span style="white-space:pre"></span> document.write(" ");
<span style="white-space:pre"></span>}
}
document.write("<br/>");
<span style="white-space:pre"></span>if(i == 5)
<span style="white-space:pre"></span>{
<span style="white-space:pre"></span>for( var j = 0; j < 2 * i + 1 ;j++ )
<span style="white-space:pre"></span>document.write("*");
}
}
11.1-1/2+1/3-1/4+........-1/100 = 1+(-1/2)+1/3+(-1/4).....+(-1/100)
var sum = 0;
for(var i = 1;i <= 100; i++){
if(i % 2 == 0){
<span style="white-space:pre"></span>sum = sum + -1 * (1 /i ) ;
}
else {
sum = sum + 1 / i ;
}
}
document.write(sum);