关于for循环的简单练习
1.打印100以内,7的倍数或者包含7的数字,并统计符合要求的数量
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var sum=0;
document.write("打印100以内,7的倍数或者包含7的数字,并统计符合要求的数量<br>");
for (var i = 0; i <= 100; i++) {
if (i % 10 == 7 || i % 7 == 0 || parseInt(i / 10 == 7)) {
sum+=1;
document.write(i+" ");
}
}
document.write("<br>"+"符合要求的个数一共有"+sum+"个");
</script>
</body>
</html>
2.打印1000以内的水仙花数
即个位数的立方+十位数的立方+百位数的立方=它本身
例:111+555+333=153
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
document.write("一千以内的水仙花数:")
for (var i = 100; i < 1000; i++) {
var a = parseInt(i / 100);
var b = parseInt(i % 100 / 10);
var c = i % 10;
if ((a * a * a + b * b * b + c * c * c) == i) {
document.write(i+" ");
}
}
</script>
</body>
</html>
3.猴子每天吃总数香蕉的一半,并扔掉剩余中的一个坏的,第七天猴子睁眼时,发现还有一个香蕉,问第一天有几个香蕉。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
var sum = 1;
for (var i = 1; i < 7; i++) {
sum = (sum + 1) * 2;
}
document.write("猴子一共吃了"+sum+"个桃子");
</script>
</body>
</html>
4.点击按钮,自动添加新闻
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
div {
border: 2px blue solid;
}
</style>
</head>
<body>
<button id="btn">点我添加新闻</button>
<div></div>
<script>
var oBtn = document.getElementById("btn");
var arr = ["数控技网络营销培训,成功转行入职云*集团", "年薪体验", "美国疫情破1000万", "天猫销售额5千亿", "川建国同志解聘国防部长"];
var oUl = document.getElementsByTagName("div")[0];
oBtn.onclick = function () {
for(var i=0;i<arr.length;i++){
oUl.innerHTML+="<li>"+arr[i]+"</li>";
}
}
</script>
</body>
</html>