1.JS编写判断是否为质数
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <script type="text/javascript"> 8 var num = 3; 9 flag = true; 10 for(var i = 2;i < num - 1;i++){ 11 if(num%i==0) 12 flag = true; 13 break; 14 } 15 16 if(flag){ 17 alert("是质数"); 18 }else{ 19 alert("不是质数"); 20 } 21 </script> 22 <body> 23 24 </body> 25 </html>
2.JS计算多个数的和
1 <!doctype html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6 </head> 7 <script type="text/javascript"> 8 var sum = function(){ 9 var result = 0; 10 for(var i = 0;i < arguments.length;i++){ 11 result+=arguments[i]; 12 } 13 return result; 14 } 15 alert(sum(1,2,3,4,5)); 16 </script> 17 <body> 18 19 </body> 20 </html>