逆战班!js循环案例
Posted lanbai
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了逆战班!js循环案例相关的知识,希望对你有一定的参考价值。
##打印乘法表
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> for( var j =1 ; j<10; j++){ for( var i = 1; i <=j; i++){ document.write(j,‘X‘,i,‘=‘,j*i,‘  ‘) } document.write(‘<br>‘) } </script> </body> </html>
打印结果:
1X1=1
2X1=2 2X2=4
3X1=3 3X2=6 3X3=9
4X1=4 4X2=8 4X3=12 4X4=16
5X1=5 5X2=10 5X3=15 5X4=20 5X5=25
6X1=6 6X2=12 6X3=18 6X4=24 6X5=30 6X6=36
7X1=7 7X2=14 7X3=21 7X4=28 7X5=35 7X6=42 7X7=49
8X1=8 8X2=16 8X3=24 8X4=32 8X5=40 8X6=48 8X7=56 8X8=64
9X1=9 9X2=18 9X3=27 9X4=36 9X5=45 9X6=54 9X7=63 9X8=72 9X9=81
##打印*号金字塔
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> var a = prompt() for(var b=0;b<=a;b++){ for(var c=0;c<=b;c++){ document.write(‘*‘) } document.write(‘<br>‘) } </script> </body> </html>
打印结果:
*
**
***
****
*****
******
##阶乘累加
1!+2!+3!+...+n!
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> var res = 0 for(var j=1;j<=3;j++){ var a = 1 for(var i=1;i<=j;i++){ a*=i } res += a } console.log(res) </script> </body> </html>
##阿基米德与国王的故事
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <script> var res = 1 var sum = 1 for(var i=1;i<64;i++){ res = res*2 sum = sum += res } // console.log((sum/100000).toFixed(5)) console.log(sum) </script> </body> </html>
结果:18446744073709552000 (国王输惨了)
以上是关于逆战班!js循环案例的主要内容,如果未能解决你的问题,请参考以下文章