Logo

Programming-Idioms

History of Idiom 195 > diff from v11 to v12

Edit summary for version 12 by aj:
[JS] To add caller

Version 11

2021-08-16, 15:22:40

Version 12

2021-08-16, 15:23:19

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Idiom #195 Pass a two-dimensional array

Pass an array a of real numbers to the procedure (resp. function) foo. Output the size of the array, and the sum of all its elements when each element is multiplied with the array indices i and j (assuming they start from one).

Variables
a,foo,i,j
Variables
a,foo,i,j
Code
/**
 * @param {Array<Array>} arry
 *
 * @return {Array<Array>}
 */
function foo(arry) {
  let len = 0;
  let sum = 0;

  arry.forEach(function(base, i) {
    len += base.length;

    base.forEach(function(a, j) {
      sum += a * (i + 1) * (j + 1);
    });
  });

  console.log('Array size:', arry.length, ',', len);

  return sum;
}
Code
/**
 * @param {Array<Array>} arry
 *
 * @return {Array<Array>}
 */
function foo(arry) {
  let len = 0;
  let sum = 0;

  arry.forEach(function(base, i) {
    len += base.length;

    base.forEach(function(a, j) {
      sum += a * (i + 1) * (j + 1);
    });
  });

  console.log('Array size:', arry.length, ',', len);

  return sum;
}

foo(arry2d);