Home >Web Front-end >JS Tutorial >JavaScript determines whether a number is a narcissus number
The Narcissus number refers to an n-digit number (n≥3), the sum of the nth power of the digits in each digit is equal to itself. Let me share with you through this articleJSTo determine whether a number is a narcissus number, friends who need it can refer to it
The narcissus number refers to an n-digit number ( n≥3), the sum of the nth power of the numbers on each digit is equal to itself.
For example: 1^3 + 5^3+ 3^3 = 153
//判断一个数是否数水仙花数 var num=prompt('请输入一个数字'); //得到位数可以计算幂数 var length=num.length; //使用字符串的方法获取每一位数 var content=num.split(""); //判断开始输入的数字和计算出来的结果是否相等 var result=0; for(var i=0;i<content.length;i++){ result+=Math.pow(content[i],length) } alert(result==num?'这个是水仙花数':'不是水仙花数')
The above is the JS introduced by the editor to determine whether a number is a narcissus number. I hope it will be helpful to you. This is helpful to everyone. If you have any questions, please leave me a message and I will reply to you in time!
The above is the detailed content of JavaScript determines whether a number is a narcissus number. For more information, please follow other related articles on the PHP Chinese website!