Armstrong of a number javascript
Armstrong of a number javascript
I need to know whether the entered number is an Armstrong number or not, but my code does not work for every number. Let me know what I missed. Are there any other ways?
var z, e, x, d = 0;
var b = prompt("Enter a number");
x=b;
while (z > 0)
e = x % 10;
x = parseInt(x/10);
d = d + (e*e*e);
if (b==d) alert("given no is amstrong number");
else alert("given no is not an amstrong number");
<!DOCTYPE HTML>
<html>
<head>
<title>Armstrong</title>
</head>
<body>
</body>
</html>
z
An Armstrong number being a number that is equal to the sum of the cubes of each digit? (pages.mtu.edu/~shene/COURSES/cs201/NOTES/chap04/arms.html) I don't think that's what your algorithm does.
– piisexactly3
Aug 31 '17 at 17:03
@Filburt where is
z initialized to 0? The initial declaration statement only initializes d.– Pointy
Aug 31 '17 at 17:05
z
d
Also, even if
z were initialized, the loop doesn't change the value of z anyway.– Pointy
Aug 31 '17 at 17:06
z
z
@Filburt try it yourself. That declaration statement only initializes
d. Each variable in a var declaration needs its own initialization expression in order to be initialized.– Pointy
Sep 1 '17 at 12:07
d
var
4 Answers
4
You can try for This, May this help for you:
<!DOCTYPE HTML>
<html>
<head>
<title>Armstrong</title>
</head>
<body>
</body>
</html>
<script>
var z,e,x,d=0;
var b=prompt("Enter a number");
x=parseInt(b);
while(x>0) //Here is the mistake
e=x%10;
x=parseInt(x/10);
d=d+(e*e*e);
console.log("d: "+d+" e: "+e);
if(parseInt(b)==d)
alert("given no is amstrong number");
else
alert("given no is not an amstrong number");
</script>
Thank you so much. I got it
– SReddy
Aug 31 '17 at 17:11
It's ok @SReddy
– Kunvar Singh
Aug 31 '17 at 17:12
@SReddy The code above sort of works only when the entered number has 3 digits, otherwise the algorithm is wrong. Just try it with a longer Armstrong number like 1634.
– ninjin
Aug 31 '17 at 17:31
I think the way you compute the result is wrong. According to Wikipedia, an Armstrong number, also called narcissistic number, has the following property:
[An Armstrong number] is a number that is the sum of its own digits each raised to the power of the number of digits.
You can compute it like this:
var number = prompt("Enter a number");
var numberOfDigits = number.length;
var sum = 0;
for (i = 0; i < numberOfDigits; i++)
sum += Math.pow(number.charAt(i), numberOfDigits);
if (sum == number)
alert("The entered number is an Armstrong number.");
else
alert("The entered number is not an Armstrong number.");
Here is an example of functional code for Armstrong Numbers.
<script>
function execute()
var num1 = document.getElementById("Number1").value;
var num2 = document.getElementById("Number2").value;
var num3 = document.getElementById("Number3").value;
var concatNum = num1 + num2 + num3;
var num1Sqrt = num1 * num1 * num1;
var num2Sqrt = num2 * num2 * num2;
var num3Sqrt = num3 * num3 * num3;
if (num1Sqrt + num2Sqrt + num3Sqrt == concatNum)
Answer.value = "The three integers you entered are Armstrong numbers.";
if (num1Sqrt + num2Sqrt + num3Sqrt != concatNum)
Answer.value = "The three integers you entered are not Armstrong numbers.";
</script>
You first set your variables to what is entered, you then concatenate the string with the three integers.
You then square the three integers and set each value to a variable. You then have a basic check for equality, if the three squared values add up to your concatenated string then you have an Armstrong number.
This is how i solved mine:
function armstrong(num)
var digits = num.toString().split('')
var realDigits = num
var a = 0
for (let i = 0; i < digits.length; i++)
digits[i] = Math.pow(digits[i], digits.length)
a += digits[i]
if (a == realDigits)
console.log("Number is armstrong")
else if (a != realDigits)
console.log("Number is not armstrong")
armstrong(371)
//feel free to pass any value here
You can copy/paste and run this code at https://www.typescriptlang.org/play/
I hope this helps someone.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Your code does not initialize
z. The loop never runs.– Pointy
Aug 31 '17 at 16:52