iDentity iDentity - 4 months ago 22
Javascript Question

Generate arrays of numbers in certain algorithm

So I need few

arrays
:

array 1 = [1,9,17,25,33,41];
array 2 = [2,10,18,26,34,42];


etc.

So each
array
adds up 8 to the last item.

But, I need to generate this dynamically (using functions in JavaScript).

Answer

var initValue = 5;
var diff = 8;
var len = 5;

function makeDiffArray(initValue, diff, len) {
  for (var i = 0, arr = []; i < len; i++) {
    arr.push(initValue);
    initValue += diff;
  }
  return arr;
}
console.log(makeDiffArray(initValue, diff, len));