Jordan Quiroz
Jordan Quiroz

Reputation: 95

angular 8 sum x number + x number for n times

i'm trying to sum a number with same number for N times, for example:

i want to sum 5 + 5 for 5 times

time 1: 5+5 = 10
time 2: 5+5 = 15
time 3: 5+5 = 20
time 4: 5+5 = 25
time 5: 5+5 = 30

trying this follow code:

sumxtimes(number,times){
    let i = 0;
      while( i < times){
       console.log(number + number)
       i++;
      }
    }

    this.sumxtimes(5,5)

the result print is: 10 10 10 10 10

i dont know what im doing wrong.

thanks in advance

Upvotes: 0

Views: 78

Answers (1)

Raja Jaganathan
Raja Jaganathan

Reputation: 36127

Since we did not increase the number so number + number remains 10 always. Here we using running counter i to make use of we can achieve the functionality with simple math logic

function sumxtimes(number, times) {
  let i = 1;
  let acc = 0;
  while (i < times + 1) {
    console.log(number + (i * number));
    i++;
  }
}

sumxtimes(5,5)

Imagine like below table

time 1: 5+(1*5) = 10
time 2: 5+(2*5) = 15
time 3: 5+(3*5) = 20
time 4: 5+(4*5) = 25
time 5: 5+(5*5) = 30

Upvotes: 1

Related Questions