SoFunction
Updated on 2025-04-07

JS determines whether a number is a daffodil

A daffodil number refers to an n-digit number (n≥3), and the sum of n-powers of the numbers on each bit is equal to itself.

For example: 1^3 + 5^3 + 3^3 = 153

//Judge whether a number counts the number of daffodils    var num=prompt('Please enter a number');
    //Get the number of bits and calculate the power number    var length=;
    //Use string method to get each digit    var content=("");
    //Judge whether the number entered at the beginning and the calculated result are equal.    var result=0;
    for(var i=0;i<;i++){
      result+=(content[i],length)
    }
    alert(result==num?'This is the number of daffodils':'Not a daffodil')

The above is the JS introduction to the editor to determine whether a number is a daffodil. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time!