文章目录
- For in 语句
- 转移语句
- 调用函数
逻辑运算符
流程控制-分支结构
判断平闰年
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript">
var year = prompt('请输入年份','');/*后面跟的是默认值*/
if(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
document.write("是闰年");
else document.write("不是闰年");
</script>
</body>
</html>
打印三角形
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<body>
<script language="javascript">
for(var i = 0;i < 10;i++){
for(var z = 10;z > i;z--)
document.write(" ");
for(var j = 0;j < i;j++)
document.write("*");
document.write("<br>");
}
document.write("<br>");
</script>
</body>
</html>
For in 语句
类似于Java中的for each语句。
转移语句
调用函数
### 判断素数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script language = "javascript">
function isprime(n){
for(var i = 2; i * i <= n;i++){
if(n % i == 0)return false;
}
return true;
}
var a = [2,3,4,5,6];
for(i in a)if(isprime(a[i]))document.write(a[i] + " ");
</script>
</head>
<body>
</body>
</html>