三角形
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10 - i; j++) {
document.write(" ");
}
for (var j = 0; j < i * 2 -1) {
document.write("* ");
}
}
document.write("<br>");
效果图:
空菱形
for (var i = 0; i < 10; i++) {
for (var j = 0; j < 10 - i; j++) {
document.write(" ");
}
for (var j = 0; j < i * 2; j++) {
if (j == 0 || j == i*2-2) {
document.write("*");
}
else {
document.write(" ");
} } }
document.write("<br>");
for (var i = 0; i < 10; i++) {
for (var j = 0; j < i; j++) {
document.write(" ");
}
for (var j = 0; j < (10 - i) * 2) {
if (j == 0 || j ++ (10-i) * 2 - 2) {
document.write("*");
}
else {
document.write(" ");
} } }
document.write("<br>");
1~100 的和
for:
var x = 0;
for (var i = 0; i <= 100; i++) {
x = x + i;
}
document.write("1到100的和是:" + x);
while:
var x = 0, i = 0;
while ( i<= 100) {
x = x + i;
i++;
}
document.write("1到100的和是:" + x);
百文买百鸡
公鸡5文钱,母鸡3文钱,小鸡1文买3只。现有一百文钱,要求正好花完,且正好买一百只鸡!有几种方法?
var x = 0;
for (var i = 0; i <= 20; i++) {
for (var j = 0; j <= 33; j++) {
for (var z = 0; z <= 100; z++) {
if (i + j + z == 100 && i*5 + j*3 + z*(1/3) == 100) {
x++;
document.write("方法" + x + "公鸡" + i + "只,母鸡" + j + "只,小鸡" + z + "只!<br>");
} } } }
document.write("<h4>共有" + x + "种方法!</h4>");
运行结果:
判断输入年份是否为闰年
<p>请输入年份:<input id = "year"><button onclick = "myf()">提交</button></p>
<p id = "p"></p>
<script>
fnuction myf() {
var x = parseInt(document.getElementById("year").value);
if ( x % 4 == 0 && x % 100 != 0 || x % 400 == 0) {
document.getElementById("p").innerHTML = x + "年是闰年!";
}
else {
document.getElement("p").innerHTML = x + "年不是闰年!";
} }
</script>
10000元存入银行,年利息为千分之三,求存入五年后本金加利息共多少
var x = 10000;
for (var i = 1; i <= 5; i++) {
x = x + x * 0.003;
}
或
var x = 10000;
var i = 1;
do {
x = x + x * 0.003;
i++;
}
while (i <= 5)
羽毛球拍 15 元,球 3 元,水 2 元。200元每种至少一个!
var x = 0;
for (var i = 1; i *15 <= 200; i++) {
for (var j = 1; j * 3 <= 200; j++) {
for (var z = 1; z * 3 <= 200; z++) {
if (i * 15 + j * 3 + z * 2 == 200) {
x++;
document.write("方法" + x + ",羽毛球拍" + i + "副,球" + j + "个,水" + z + "瓶!");
if (x % 3 == 0) {
document.write("<br>");
} } } } }
document.write("共有方法" + x + "种!");
一张无限大的纸,可以叠任何次数,已知纸后0.03毫米,对折多上次可达到珠穆朗玛峰的高度8848:
var x = 0.03;
for (var i = 1; ; i++) {
x = x * 2;
if (x >= 8840000) {
break;
}
}
document.write("对折" + i + "次,高" + Math.round(x/1000) + "米!");
12 ( ) 34 ( ) 56 ( ) 78 ( ) 9 = 59,在括号内添加加减号使等式成立!
var x = 0;
for (var i = -1; i < 2; i += 2) {
for (var j = -1; j < 2; j += 2) {
for (var z = -1; z < 2; z += 2) {
for (var y = -1; y < 2; y += 2) {
if (12 + i*34 + j*56 + z*78 + y*9 == 59) {
document.write("12 + (" + i*34+ " ) + ( " + j*56 +") + (" + z*78 + ") + (" + y*9 + ") = 59");
x++;
} } } } }
document.write("共有" + x + "种方法!");
有一对幼兔,幼兔一个月后长成小兔,小兔一个月后长成成兔并生下一对幼兔。问8个月后有多少对兔子,幼兔,小兔,成兔分别有几只!
var x = 0, y = 0, z = 1;
for (var i = 1; i <= 8; i++) {
x = x + y;
y = z;
z = x;
document.write("幼兔有" + x + "只,小兔有" + y + "只,成兔有" + z + "只,总共有" + (x + y + z) + "只兔子!");
}
document.write("8个月后,幼兔有" + x + "只,小兔有" + y + "只,成兔有" + z + "只,总共有" + (x + y + z) + "只兔子!");
凑钱:1,2,5凑够20元钱
var x = 0;
for (var i = 0; i*1 <= 20; i++) {
for (var j = 0; j*2 <= 20; j++) {
for (var z = 0; z*5 <= 20; z++){
if (i * 1 + j * 2 + z * 5 == 20) {
x++;
document.write("方法" + x + ":1元" + i + "张,2元" + j + "张,5元" + z + "张! ");
if (x % 3 == 0) {
document.write("<br>");
} } } } }
document.write("<h4>共有方法" + x + "种!</h4>");
计算1~100之间所有不能被3整除的整数的和大于或等于2000的数字!
var x = 0;
for (var i = 1; i <= 100; i++ ) {
if (i % 3 != 0) {
x = x + i;
}
if (x >= 2000) {
document.write(x);
break;
}
}
水仙花数
var x = 0, y = 0, z = 0, j = 0;
for (var i = 100; i < 1000 ; i++) {
x = parseInt(i % 10);
y = parseInt(i /10 %10);
z = parseInt(i / 100);
if (z*z*z + y*y*y + x*x*x == i) {
j++;
document.write("第" + j + "个,水仙花是" + i + "<br>");
}
}
document.write("一共有"++"个水仙花数!");
猴子摘若干桃子,第一天吃一半多一个,第二天又吃一半多一个......到第十天发现桃子只剩一个!问猴子摘了多少桃子
var x = 1;
for (var i = 9; i >= 1; i++) {
x = (x + 1) * 2;
document.write("猴子第" + i + "天还剩" + x + "个桃子!");
}
document.write("猴子第一天摘了" + x + "个桃子!");
1-20关每关 1 分,21-30关每关 10 分,31-40关每关 20 分,41-49关没关 30 分,第50关100分!输入关卡数获得分数
<p>请输入关卡数:<input id = "guan"><button onclick = "qia()">提交</button></p>
<p id = "gq"></p>
<script>
function qia() {
var x = parseInt(docnment.getElementById("guan").value);
var y = 0;
if (x == 50) {
y = 100 + 270 + 200 + 100 +20; // 100 + 9*30 + 10 * 20 + 10 *10 + 20 *1
document.getElementById("gq").innerHTML = "恭喜你获得" + y + "分!";
}
else if (x < 50 && x > 40) {
y = (x - 40) * 30 + 320;
document.getElementById("gq").innerHTML = "恭喜获得" + y + "分!";
}
else if (x <= 40 && x > 30) {
y = ( x - 30) * 20 + 120;
document.getElementById("gq").innerHTML = "恭喜获得" + y + "分!";
}
else if (x <= 30 && x > 20) {
y = (x - 20) * 10 + 20;
document.getElementById("gq").innerHTML = "恭喜获得" + y + "分!";
}
else if ( x <= 20 && x >= 1) {
y = x;
document.getElementById("gq").innerHtml = "恭喜获得" + y + "分!";
}
else {
document.getElementById("gq").innerHTML = "请输入1~50的关卡数";
}
}
</script>