14,937,163 members
See more:
HTML
```I am getting a false output...

The code is given below...

<html>
<title> My test calculated cookie </title>

<script type="text/javascript">
function myFunction()
{
var num1=371;
var num2;
num2=num1;
var rem=0;
var cube=0;
var sum=0;

while(num1!=0)
{
rem=num1%10;
cube=rem*rem*rem;
sum=sum+cube;
num1=num1/10;
}

if(num2==sum)
{
}
else
{
}

}

</script>

<h1>hi</h1>
</body>
</html>```

What i am getting ...
"The number is not armstrong"
whereas it is an armstrong number...

Anyone can help me please where I am doing the mistake....
Posted

## Solution 1

Your problem is that `num1 = num1 / 10` will give you 37.1 and not 37. The `Math.floor()` function should help you out there.
enhzflep 11-Oct-12 8:35am

Bingo! Not sure which is the faster, though you can also use:

rem=(num1>>0)%10;

(num1>>0) converts a floating point number to an int. It truncates, as floor does.
Graham Breach 11-Oct-12 9:08am

Good point - I tend to use "~~variable" for truncating in Javascript and another method I've seen is "variable|0".
enhzflep 11-Oct-12 9:16am

Hmmm. Thank-you. 2 more tricks to add to the toolbox. :)

## Solution 2

I have got the solution...

JavaScript
```function myFunction(myname,passkey)
{
var num1 = passkey;
var num2;
num2=num1;
var rem=0;
var cube=0;
var sum=0;

while(num1 != 0)
{
rem = parseInt(num1 % 10);
cube = rem * rem * rem;
sum = sum + cube;
num1 = parseInt(num1 / 10);
}

if(num2==sum)
{
}
else
{
}

}```

You were right Graham, its generating decimal number which creating further iterations to the loop..