简体   繁体   中英

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).
  • Example: Given 5. So the series will become (3 + 33 + 333 + 3333 + 33333). Expected output: 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. 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:

 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. Please check the snippet for the solution by using the padEnd() method. In this way, you can avoid string concatenation or extra loop.

Please check the link to know more about the method 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!!

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

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].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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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