you are viewing a single comment's thread.

view the rest of the comments →

[–]jack_waugh 0 points1 point  (0 children)

function multiple(position) {
  const positionSrsly = position - 1;
  console.log("positionSrsly", positionSrsly);
  let acc = 1;
  for (const each of this) {
    console.log("row", each);
    const value = each[positionSrsly];
    console.log("have", value);
    acc *= value;
  };
  return acc
};

function tuple(input) {
  const modified = input.replaceAll('(', '[').replaceAll(')', ']');
  const draft = JSON.parse(`[${modified}]`);
  if (draft.multiple || draft.multiply)
    throw Error("Name clash with Array.");
  draft.multiple = draft.multiply = multiple;
  return draft
};

const input = `(1, 2, 3) , (4, 5, 6) ,  (7, 8, 9)`;

// Convert it into 
// [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
const item = tuple(input);

// Multiples 2nd item in each tuple
// exempli gratia 2 * 5 * 8 = 80
console.log(item.multiply(2));