Create the code to fill in the vector - Javascript / Logic

0

I have a problem with an exercise on arrays. The question is this:

Create the code to populate the array with the following values:    3 4 7 12 19 28 39 52 67 84

I've done the other exercises on this list, my problem with this is in logic. I can not think of how to increment the counter so that it adds the next odd number. I even tried to elaborate as follows:

if((i+1)%2==0)
{
     numero = numero+i;
     contador= i;
}
else {
     numero= numero+2;
     contador =  i;
}

But it did not. Any help ???

    
asked by anonymous 19.07.2018 / 01:07

1 answer

0

Resolution:

let n = 3;
let i = 1;
let a = [];

while(n <= 84){
    a.push(n);
    n += i;
    i += 2;
}

console.log(a);
    
19.07.2018 / 03:27