练习for循环
- 打印1-100之间所有7的倍数的个数及总和
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循环练习2</title>
<script type="text/javascript">
var sum=0;
var count=0;
for(var i=1; i<=100;i++){
if(i%7==0){
sum+=i
count++
}
}
alert("1-100之间7的倍数的总和:"+sum)
alert("1-100之间7的倍数的个数:"+count)
</script>
</head>
<body>
</body>
</html>
360截图20180917215932726.jpg
360截图20180917215939827.jpg
2.九九乘法表
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>九九乘法表</title>
<script type="text/javascript">
for(var row=1;row<=9;row++){
for(var col=1; col<row+1;col++){
var x=rowcol;
document.write(col+""+row+"="+x+""+"");
}
document.write("
");
document.write("<br/>");}
</script>
</head>
<body>
</body>
</html>
360截图20180917220113247.jpg
3.打印1-100之间所有奇数之和
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循环练习1</title>
<script type="text/javascript">
var sum=0;
var i=1;
while(i<100){
if(i%2 != 0){
sum+=i;
}
i++
}
alert("100内奇数和"+sum)
// var sum = 0;
// for(var i=1;i<=100;i++)
// {
// if(i%2 !==0){
// sum+=i;
// }
// }
// alert("所有奇数和:"+sum);
</script>
</head>
<body>
</body>
</html>
360截图20180917220254930.jpg
4.1~100之间的质数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>所有质数</title>
<script type="text/javascript">
var num = []
for(var i=2; i<=100;i++){
var count=1;
for(var j=2; j<parseInt(i/2);j++){
if(i%j==0){
count = 0;
break;
}
}
if(i>2&&count==1){
num.push(i)
} }
alert(num)
</script>
</head>
<body>
</body>
</html>
360截图20180917220821670.jpg
5.判断质数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>判断质数</title>
<script type="text/javascript">
var num = prompt("请输入一个大于1的整数:")
if(num<=1 || isNaN(num)){
alert("输入的值不合法");
}
else{ var flag = true;
for(var i=2;i<num;i++){
if(num%i == 0){
flag = false;
alert( num+"不是质数") }
}
if(flag){alert(num+"是质数")}
}
</script>
</head>
<body>
</body>
</html>
360截图20180917221040347.jpg
6.水仙花数
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>循环练习2</title>
<script type="text/javascript">
for(var i=100; i<=999;i++){
var bai = parseInt(i/100);
var shi = parseInt(i%100/10);
var ge = parseInt(i%10)
if(bai3+ shi3 + ge**3 == i){
alert(i)
}
}
</script>
</head>
<body>
</body>
</html>
360截图20180917221213123.jpg
360截图20180917221221348.jpg
360截图20180917221228945.jpg
360截图20180917221236757.jpg
7.编写程序,由键盘输入三个整数分别存入变量num1、num2、num3,对他们进行排序,并且从小到大输出。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>if练习3</title>
<script type="text/javascript">
var num1 = prompt("请输入一个数字:")
var num2 = prompt("请输入一个数字:")
var num3 = prompt("请输入一个数字:")
if(num1<num2 && num1<num3){
if(num2<num3){
alert(num1+","+num2+","+num3);
}else{
alert(num1+","+num3+","+num2);
}
}else if(num2<num1 &&num2<num3){
if(num1<num3){
alert(num2+","+num1+","+num3)
}else{ alert(num2+","+num3+","+num1);
}
}else{
if(num1<num2){
alert(num3+","+num1+","+num2);
}else{
alert(num3+","+num2+","+num1);
}
}
</script>
</head>
<body>
</body>
</html>