奇数之和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>奇数之和</title>
<script type="text/javascript">
for(var i=1,sum=0; i<=100; i++){
if(i % 2 != 0){
sum += i;
}
}
console.log("奇数之和为:" + sum);
</script>
</head>
<body>
</body>
</html>
7的倍数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>7的倍数</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++;
}
}
console.log("总和为:" + sum);
console.log("个数为:" + count);
</script>
</head>
<body>
</body>
</html>
水仙花数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水仙花数</title>
<script type="text/javascript">
for(var i=100; i<1000; i++){
var b = parseInt(i/100);
var s = parseInt((i-b*100)/10);
var g = i % 10;
if(b**3 + s**3 + g**3 == i){
console.log(i);
}
}
</script>
</head>
<body>
</body>
</html>
判断是否为质数
<!DOCTYPE html>
<html lang="en">
<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;
}
}
if(flag){
alert(num + "是质数!");
}else{
alert(num + "不是质数!");
}
}
</script>
</head>
<body>
</body>
</html>
99乘法表
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>99乘法表</title>
<style type="text/css">
body{
width: 1000px;
}
span{
display: inline-block;
width: 70px;
}
</style>
<script type="text/javascript">
for(var i=1; i<=9; i++){
for(var j=1; j<=i; j++){
document.write("<span>"+j+"*"+i+"="+i*j+"</span>");
}
document.write('<br>');
}
</script>
</head>
<body>
</body>
</html>
1~100之间的质数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>1-100之间所有的质数</title>
<script type="text/javascript">
console.time("test");
for(var i=2; i<=100; i++){
var flag = true;
for(var j=2; j<=Math.sqrt(i); j++){
if(i % j ==0){
flag = false;
break;
}
}
if(flag){
console.log(i);
}
}
console.timeEnd("test");
</script>
</head>
<body>
</body>
</html>