综合题5_编程题8.html 524 B

12345678910111213141516171819202122232425
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. function foo(n){
  11. var res = 0;
  12. for(var i=1;i<=n;i++){
  13. res += Math.pow(-1,i-1)*(1/(2*i-1));
  14. }
  15. res *= 4;
  16. return res;
  17. }
  18. console.log(foo(100));
  19. console.log(foo(500));
  20. </script>
  21. </body>
  22. </html>