JS之for循环实现1-100之间的奇数、偶数和
奇数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>1-100之间的奇数和</title>
<link rel="stylesheet" href="">
<script>
var sum=0;
for(var i=1;i<=100;i++){
if(i%2==1){
sum+=i;//sum=sum+i;
}
}
document.write('1-100之间的奇数和为:'+sum);
</script>
</head>
<body>
</body>
</html>
偶数
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>1-100之间的偶数和</title>
<link rel="stylesheet" href="">
<script>
var sum=0;
for(var i=1;i<=100;i++){
if(i%2==0){
sum+=i;
}
}
document.write('1-100之间的偶数和为:'+sum);
</script>
</head>
<body>
</body>
</html>
标签:JavaScript