1到100之間所有奇數(shù)之和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
</body>
<script>
var sum = 0;
for(var i = 1; i <= 100; i++){
if(i % 2!= 0){
sum += i;
}
}
document.write(sum+"<br>");
sum = 0;
for(var i=1;i<100;i+=2){
sum+=i;
}
document.write(sum);
</script>
</html>
1-100之間所有7的倍數(shù)的個(gè)數(shù)及總和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<script type="text/javascript">
function sum(){
var sum = 0;
for(var i = 1; i < 100; i++){
if(i % 7 == 0){sum+=i;}
}
return sum;
}
document.write('總和:'+sum())
</script>
<script type="text/javascript">
function num(){
var num = 0;
for(var i = 1; i < 100; i++){
if(i % 7 == 0){num++;}
}
return num;
}
document.write('計(jì)數(shù):'+num())
</script>
</body>
</html>
1-100質(zhì)數(shù)求和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>1-100質(zhì)數(shù)求和</title>
</head>
<body>
<div id="wrap"></div>
<script type="text/javascript">
var wrap = document.getElementById("wrap");
var str = "";
for (var i = 2; i <= 100; i++) {
var count = 0;
for (var j = 1; j <= i; j++) {
if( i%j ==0 ){
count++;
}
}
if (count == 2) {
str += "<span>" + i + "</span><br>";
}
count = 0;
}
wrap.innerHTML = str;
</script>
</body>
</html>
判斷質(zhì)數(shù)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>質(zhì)數(shù)</title>
</head>
<body>
<script type="text/javascript">
var num =Number(prompt("請(qǐng)輸入一個(gè)正整數(shù),判斷此數(shù)是不是素?cái)?shù)"));//要判斷的數(shù)
var flag = true;//假設(shè)變量 值為true 是素?cái)?shù)
for( var i = 2 ; i < num ; i++ ){
if( num % i == 0 ){//滿足這個(gè)條件 說(shuō)明有因子了 不是素?cái)?shù)
flag = false;//不是素?cái)?shù)
break;
}
}
//如果假設(shè)成立
if( flag && num != 1 ){ //1不是素?cái)?shù)
document.write( num + "是素?cái)?shù)" );
}else{
document.write( num + "不是素?cái)?shù)" );
}
</script>
</body>
</html>
乘法口訣表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>乘法口訣表</title>
</head>
<body onload="step()">
<script type="text/javascript" >
function step(){
for(i=1;i<=9;i++){
for(j=1;j<=i;j++){
document.write(i+"x"+j+"="+i*j +"   ");
}document.write("<br/>");
}
}
</script>
</body>
</html>
水仙花數(shù)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水仙花數(shù)</title>
</head>
<body>
<script type="text/javascript">
var a, b, c;
for (var i = 100; i < 1000; ++i) {
a = i % 10;
b = ((i / 10) % 10);
b = parseInt(b);
c = i / 100;
c = parseInt(c);
if (i == a * a * a + b * b * b + c * c * c)
document.write(i + "<br />");
}
</script>
</body>
</html>