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

JavaScript determines whether a number is a narcissus number

陈政宽~
陈政宽~Original
2017-06-28 13:05:274056browse

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;)

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!

Statement:
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