n*n方块:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- //n的值可以控制
- var n=10;
- var str="";
- //外循环:控制行数
- for(var i=1;i<=n;i++){
- //内循环:每行打印几个,每循环一次打印出一颗星星
- //j=i:下一行打印出的星星数等于第一行打印的星星数减行数
- for(var j=1;j<=n;j++){
- str=str+"*";
- }
- str=str+"\n";
- }
- console.log(str);
- </script>
- </head>
- <body>
-
- </body>
- </html>
倒三角:
区别:内循环时j=i,循环次数等于第一行数目 - 行数
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- //n的值可以控制
- var n=10;
- var str="";
- //外循环:控制行数
- for(var i=1;i<=n;i++){
- //内循环:每行打印几个,每循环一次打印出一颗星星
- //j=i:下一行打印出的星星数等于第一行打印的星星数减行数
- for(var j=i;j<=n;j++){
- str=str+"*";
- }
- str=str+"\n";
- }
- console.log(str);
- </script>
- </head>
- <body>
-
- </body>
- </html>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- <script>
- var x="";
- for(var i=1;i<=9;i++){
- for(var j=1;j<=i;j++){
- //1×2=2
- x+=i+"x"+j+"="+i*j+" ";
- }
- x+="\n";
- }
- console.log(x);
- </script>
- </head>
- <body>
-
- </body>
- </html>