循环语句练习。
题目:
1、编写程序输入任意一个数,求阶乘。
2、编写程序输出1!+2!+3!+……+6!的结果。
3、输出乘法口诀表,以倒的形式输出。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>计算阶乘</title> <script type="text/javascript"> var n = text.value; function factorial(n){ var factorial = 1; for(var i=1; i<=n; i++){ factorial = factorial * i; } alert(n + "!=" + factorial); } </script> </head> <body> <form> <input type="text" name="text" placeholder="请输入正整数"/> <input type="button" name="button" value="计算阶乘" onclick="factorial(parseInt(text.value))"/> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>1!+2!+3!+……+6!的结果</title> <script type="text/javascript"> function factor(num){ var sum = 0, factor = 1; for(var i=1; i<=num; i++){ factor = factor * i; sum = sum + factor; } return sum; } </script> </head> <body> <p> <script> document.write("1!+2!+3!+……+6!=",factor(6)); </script> </p> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>乘法口诀表</title> <script type="text/javascript"> for(var i=9; i>=1; i--){ for(var j=i; j>=1; j--){ document.write(i+"*"+j+"="+i*j+" "); } document.write("<br>"); } </script> </head> <body> </body> </html>
实验存档。