水仙花数是指一个三位数,他的每个位上的数字的3次幂之和等于它本身。下面我将讲解如何用js做一个水仙花数,代码如下:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script type="text/javascript">
var i=prompt("请输入一个三位数");
var a=i%10; //取个位数
var b=parseInt(i/10)%10; //取十位数
var c=parseInt(i/100); //取百位数
if(i==Math.pow(a,3)+Math.pow(b,3)+Math.pow(c,3))
{
alert("yes");
}
else
{
alert("no");
}
</script>
</head>
<body>
</body>
</html>
其中,Math.pow(x,y)表示、x的y次方