English 中文(简体)
印有 Java印的军机号
原标题:Verify is Armstrong number with Javascript
  • 时间:2017-08-31 16:48:45
  •  标签:
  • javascript

我需要制定一部法律,以核实所输入的数字是否是军机,但我的法典并不针对每个数字。

谁能告诉我我我我我失踪了吗? 是否有其他办法这样做?

Thank you!

let e, x, d = 0;
let b = prompt("Enter a number");
x=b;

while (x > 0) {
  e = x % 10;
  x = parseInt(x/10);
  d = d + (e*e*e);
}

if (b==d)
   alert("given number is an armstrong number");
else
   alert("given number is not an armstrong number");
<!DOCTYPE HTML>
<html>
<head>
        <title>Armstrong</title>
</head>
<body>
</body>
</html>
问题回答

我认为,你计算结果的方式是错误的。 据Wikipedia称,军机号(也称职号)拥有以下财产:

[军机号]是数,即每升数的本国数字的总和。

你可以赞扬它这样做:

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.");
}

你可以尝试这一方法,很容易理解。

const armstrongNumber = (num) => {

    const toArray = num.toString().split(  ).map(Number);

    const newNum = toArray.map(a => {return a**3}).reduce((a, b) => a + b);

    if(newNum == num){
        console.log( This is an armstrong number );
    }else{
        console.log( This is not an armstrong number );

    }
}
armstrongNumber(370);
//This is an armstrong number

这里的解决办法是,在不使用Math Object的情况下检查军校人数。

function armstrongNum(number) {
    const   numberArr = String(number).split(  );
    const power = numberArr.length;
    let TotalSum = numberArr.reduce((acc, cur) => {
      return acc + (function(cur,power){
        let curNum = Number(cur);
        let product = 1;
        while(power > 0) {
          product *= curNum;
          power --;
        }
        return product;
      }(cur,power))
    }, 0)
    if (TotalSum === number) {
      return true
    }
    return false
}
armstrongNum(153);

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>

你先把你们的变数确定到所进入的东西上,然后,你会把这三根ger合起来。

然后,你确定三个分类账,并将每个数值确定为变量。 如果三个平方位的数值加上你精心策划的扼杀,那么你就有了平等的基本检查,那么你就拥有一个军机。

这是如何解决地雷问题:

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.

这也是行之有效的。

function isArmstrong (n) {
    const res = parseInt(n, 10) === String(n)
        .split(  )
        .reduce((sum, n) => parseInt(sum, 10) + n ** 3, 0);
    console.log(n,  is , res,  Armstrong number )
    return res
}

isArmstrong(153)

这是解决这一问题的另一个途径。

let c = 153
let sum = 0;

let d = c.toString().split(  );

console.log(d)

for(let i = 0; i<d.length; i++) {
   sum = sum + Math.pow(d[i], 3)
}
if(sum == c) {
    console.log("armstrong")
}
else {
    console.log("not a armstrong")
}

3. 找到航天员的正确途径

var e, x, d = 0, size;
var b = prompt("Enter a number");
b=parseInt(b);
x=b;
size = x.toString().length;
while (x > 0) {
  e = x % 10;
  d = d + Math.pow(e,size);
  x = parseInt(x/10);
}

//This is I solved without function
let num = prompt();
let num1 = num;
let sum = 0;
while(num > 0){
    rem = num % 10;
    sum = sum + Math.pow(rem, num1.length);
    num = parseInt (num /10); 
}
if (sum == num1) console.log("Armstrong");
else console.log("not Armstrong");

编号为军阀。

 

let inputvalue=371
let spiltValue=inputvalue.toString().split(  )
let output=0
spiltValue.map((item)=>output+=item**spiltValue.length)
alert(`${inputvalue} ${inputvalue==output? "is armstrong number" : "is not armstrong number"}`);

In order to get a Narcissistic/Armstrong number, you need to take the length of the number as n for taking the power for summing the value.

Here s another solution that works with an input >= 3 digits (With Math.pow() each character is added as a number to the power of the array size)

const isArmstrongNumber = (n) => {
  //converting to an array of digits and getting the array length
  const arr = [...`${n}`].map(Number);
  let power = arr.length;

  const newNum = arr
    .map((a) => {
      return Math.pow(parseInt(a), power);
    })
    .reduce((a, b) => a + b);

  return newNum == n;
};

console.log("Is it Armstrong? " + isArmstrongNumber(370));
console.log("Is it Armstrong? " + isArmstrongNumber(123));
console.log("Is it Armstrong? " + isArmstrongNumber(1634));

如果没有利用任何职能,这可能有助于你的产出。

let armstrongNumber =  153 ;
armstrongNumber = isNaN(armstrongNumber) ? armstrongNumber : armstrongNumber.toString();
let sumationValue = 0;
let result = (function () {
  for (var digit of armstrongNumber) {
    sumationValue += parseInt(digit) ** armstrongNumber.length;
  }
  return sumationValue;
})();

if (result == armstrongNumber) {
  console.log(armstrongNumber +   is an Armstrong Number );
} else {
  console.log(armstrongNumber +   is not an Armstrong Number );
}




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签