Home > Web Front-end > JS Tutorial > JavaScript determines whether a number is a narcissus number

JavaScript determines whether a number is a narcissus number

陈政宽~
Release: 2017-06-28 13:05:27
Original
4077 people have browsed it

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?&#39;这个是水仙花数&#39;:&#39;不是水仙花数&#39;)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template