Sum of pairs typed interval

Asked

Viewed 1,891 times

2

I am trying to do a function for summing even numbers in an interval. For example somaDosPares(6) resultado = 12, but always returns me the number of ().

function somaDosPares(x){
    let soma = 0;
    for(let i = 0; i <= x; i += 2){
        if (i % 2 === 0){
          return soma + x
        }
    }
}

4 answers

6

There are some problems with the code.

Must accumulate the values of i and not of x. Actually it was not accumulating, it was just adding up the initial value with the first value of the loop that is 0 also so it always gives 0. It is not performing several steps at the time it finds the return the code closes, gives the result and does nothing else. Then you can only give the returnat the end of the loop when already executed everything.

Another problem in code that does not generate error but is inefficient and meaningless is the if to check if the number is even, it is already known that it is, you are getting it at 0 which is even and is adding 2 at each step, so all number of i is par.

Then you only need to boot the Accumulative Valve (soma), the loop that will go through all the desired numbers, already skipping the odd with the o += 2 that you used and return the accumulator variable. Note that to go accumulating uses the operator += you already know how to use, so the variable soma it always happens to be her value plus the new value to add up, so you do it with soma the same thing you did with i.

function somaDosPares(x) {
    let soma = 0;
    for (let i = 0; i <= x; i += 2) soma += i
    return soma;
}

console.log(somaDosPares(6));

I put in the Github for future reference.

You have to solve this performatively, but to solve your algorithm you may even need to be this way to pass automatic evaluation because it’s about the for would be this there.

4

Workaround: need not even use a loop to solve the problem. Remember the basic math must have studied arithmetic progressions. Sequence 2, 4, 6 is a PA of three terms starting at 2 with ratio 2. It is easily demonstrated that the sum of a finite arithmetic progression is given by the sum of the first and last term, multiplied by half the amount of terms. That is, the sum of 2, 4 and 6 is given by (2+6)*3/2, which is 12.

When receiving the last element of the range as a function parameter, just check whether it is even or odd; if it is even it will also be the last term of PA, if it is odd the last term of PA will be the number before it. The amount of values will always be half of the last term of EF.

Therefore:

function somaDosPares(n) {
  const ultimo = (n % 2 == 0) ? n : n-1;
  const primeiro = 2;
  const quantidade = ultimo / 2;
  
  return (primeiro + ultimo) * quantidade / 2;
}

console.log(somaDosPares(6));
console.log(somaDosPares(10));

3

Is because you put the return before you finish the for, correction:

function somaDosPares(x){
    let soma = 0;
    for(let i = 2; i <= x; i += 2){                
          soma = soma + i;
    }
    return soma;
}

console.log(somaDosPares(6))

and in the accumulator is the variable i not the x and also the decision structure is unnecessary because it can already start from the number par 2 (in my case example already start variable no 2, since 0 does not influence the sum) and always increase 2, and this already accumulates the even values.

2


Since the variable i loop is incremented in +2 beginning from 0, it makes no sense to use a if to verify that the number is even, because i will always be par. And you are using the return in the wrong place and adding the variable soma with x, when it should add to i:

function somaDosPares(x){
   let soma = 0;
   for(let i = 0; i <= x; i+=2){
     soma += i
   }
   return soma;
}
console.log(somaDosPares(9));

Passing by 9 to the function, for example, will add even numbers:

0 + 2 + 4 + 6 + 8 = 20

Browser other questions tagged

You are not signed in. Login or sign up in order to post.