打印1-100之间所有奇数之和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>奇数和</title>
<script>
var sum = 0;
for(var i = 1; i <= 100; i++){
if(i % 2!= 0){
sum += i;
}
}
document.write(sum);
</script>
</head>
<body>
</body>
</html>
打印1-100之间所有7的倍数的个数及总和
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>7的倍数</title>
<script>
var sum = 0;
var count = 0;
for(var i = 0;i <=100;i++){
if(i % 7 == 0){
document.write(i);
sum += i;
count ++;
}
console.log(count);
console.log(sum);
}
</script>
</head>
<body>
</body>
</html>
水仙花数是指一个3位数,它的每个位上的数字的3 次幂之和等于它本身。
(例如:1^3 + 5^3 + 3^3 = 153),请打印所有的水仙花数。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>水仙花数</title>
<script type="text/javascript">
for(var i=100; i<1000; i++){
var bai = parseInt(i/100);
var shi = parseInt((i-bai*100)/10);
var ge = i % 10;
if(bai**3 + shi**3 + ge**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>乘法表</title>
<script>
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<=10000; 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>