简体   繁体   English

如何找到一系列重复数字的总和?

[英]How to find the sum of a series of repeated numbers?

The question I'm trying to solve is:我试图解决的问题是:

  • Find the sum of the series (3 + 33 + 333 + 3333 + ... + n), to n terms, where n is an input provided by the user (using prompt).求系列 (3 + 33 + 333 + 3333 + ... + n) 的总和,到 n 项,其中 n 是用户提供的输入(使用提示)。
  • Example: Given 5. So the series will become (3 + 33 + 333 + 3333 + 33333).示例:给定 5。因此该系列将变为 (3 + 33 + 333 + 3333 + 33333)。 Expected output: 37035预期输出:37035

This is my code:这是我的代码:

const number = 5;
let sum = 3;
let add;

for (i = 0; i <= number; i++){
  add = "";
  for (j = 1; j <= i; j++) {
    add += 3;
    sum = add * 10 + 3
  }
  sum = sum  + *5;
}
console.log(sum);

It's not giving me the desired outcome (which is obviously the issue I'm running into).它没有给我想要的结果(这显然是我遇到的问题)。 I haven't used prompt yet because I don't know how to implement it.我还没有使用过 prompt 因为我不知道如何实现它。 Could someone please help me?有人可以帮我吗?

You don't need a nested loop.您不需要嵌套循环。 Use just a single loop, and inside, increment add based on the power of 10 of the current iteration, and add the result to the sum:只使用一个循环,在内部,根据当前迭代的 10 的幂递增add ,并将结果添加到总和中:

 const count = 3 let sum = 0; let add = 0; for (i = 0; i < count; i++){ add += 3 * 10 ** i; sum += add; } console.log(sum);

You can use the padEnd() method to add padding at end of a string.您可以使用padEnd()方法在字符串的末尾添加填充。 Please check the snippet for the solution by using the padEnd() method.请使用padEnd()方法检查解决方案的片段。 In this way, you can avoid string concatenation or extra loop.通过这种方式,您可以避免字符串连接或额外的循环。

Please check the link to know more about the method padEnd 请查看链接以了解有关 padEnd 方法的更多信息

 const count = 5; let number = 3; let sum = 0; for(i = 1; i <= count; i++) { sum += Number(number.toString().padEnd(i, number.toString())); console.log(i.toString() + ". " + number.toString().padEnd(i, number.toString())); } console.log("Sum: " + sum);

Here is what you ask i think, so i turn to string the iteration number in order to add it to an array and then reduce javascript property to sum all it's elements!!这就是你问我的想法,所以我转而将迭代号串起来,以便将它添加到一个数组中,然后减少 javascript 属性以求和它的所有元素!

 const number = 5; let sum = 3; let added = ''; let add = []; sum = sum.toString(); added = sum; for(let i = 0; i<number; i++){ add.push(added); added += sum; } let result = add.reduce((ant,sum)=>{ return parseInt(ant) + parseInt(sum); }); console.log(result);

Edit: for the prompt question here's a link that i hope it helps you!编辑:对于提示问题,这里有一个链接,希望对您有所帮助!

You can use it this way: const number = prompt("enter how much numbers");你可以这样使用它: const number = prompt("输入多少数字");

Here's how you can do it:您可以这样做:

 const sumNum = 3; const number = Number(prompt('Please enter a number for repeatitions: ')); let sum = ''; for (let i = 0; i < number; i++) { for (let j = 0; j <= i; j++) { sum += sumNum; } if (i < number) sum += '+'; } let total = 0; sum = sum.split('+'); sum.pop(); //to remove last empty field while (sum.length) { total += Number.parseFloat(sum.shift()); } console.log(total);

To repeat a number:重复一个数字:

const x = 3;
Number(`${x}`.repeat(1)); //=> 3
Number(`${x}`.repeat(2)); //=> 33
Number(`${x}`.repeat(3)); //=> 333
Number(`${x}`.repeat(4)); //=> 3333
Number(`${x}`.repeat(5)); //=> 33333

So from such array [3, 3, 3, 3, 3] we can do:所以从这样的数组[3, 3, 3, 3, 3]我们可以做:

[3, 3, 3, 3, 3].map((x, i) => Number(`${x}`.repeat(i+1)));
//=> [3, 33, 333, 3333, 33333]

But we don't need to manually fill an array with n identical numbers:但是我们不需要用n 个相同的数字手动填充数组:

const n = 5;
const x = 3;
Array(n).fill(x);
//=> [3, 3, 3, 3, 3]

Or:或者:

Array.from(Array(n), () => x);
//=> [3, 3, 3, 3, 3]

But obviously if we can apply a function on each item we may as well produce the full series:但显然,如果我们可以对每个项目应用一个函数,我们也可以生成完整的系列:

Array.from(Array(n), (_, i) => Number(`${x}`.repeat(i+1)));
//=> [3, 33, 333, 3333, 33333]

Finally we can reduce the series to the sum of all numbers:最后,我们可以将系列减少到所有数字的总和:

[3, 33, 333, 3333, 33333].reduce((tot, x) => tot + x, 0);
//=> 37035

So altogether we have:所以我们总共有:

const n = 5; // use prompt() but don't forget to coerce the result into a number
const x = 3;

Array.from(Array(n), (_, i) => Number(`${x}`.repeat(i+1)))
  .reduce((tot, x) => tot + x, 0);

If you don't need to produce a series then a recursive function is perhaps simpler:如果您不需要生成一个系列,那么递归函数可能更简单:

const sum_series =
  (x, n, tot=0) =>
    n === 0
      ? tot
      : sum_series(x, n-1, tot+=Number(`${x}`.repeat(n)))

sum_series(3, 5);
//=> 37035

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM