注册 登录
编程论坛 JavaScript论坛

写一程序求一个数的平方根。

颜小培 发布于 2010-03-08 15:56, 854 次点击
谢谢大家!
2 回复
#2
aspic2010-03-08 16:25
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<script type="text/javascript">
function get(v)    {
    var a = 1;
    var b = a * a - v;
    for(var i = 0; i < 10; i++) {
        a = a - b / (2 * a);
        b = a * a - v;
    }
    document.getElementById("input").value = a;
}
</script>
</head>

<body>
<input type="text" id="input" /><input type="button" value="计算" onclick="get(document.getElementById('input').value)" />
</body>
</html>
#3
aspic2010-03-08 16:26
程序代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. xmlns="http://www. http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<input type="text" id="input" /><input type="button" value="计算" onclick="alert(Math.sqrt(document.getElementById('input').value))" />
</body>
</html>
1