/**
 * Read FAQs section on the left for more information on how to use the editor
 **/
// DO NOT CHANGE FUNCTION NAME

const DIGITS_ENCLOSED_WITH_BRACKETS_REGEX =
  /\(\s*(\d+\s*(?:,\s*\d+\s*)*)\s*\)/g;

function multiple(position) {
  // write your code below
  
  if(!position){
    throw new TypeError('Missing argument');
  }
  
  if (typeof position !== 'number') {
    throw new TypeError('Argument must be of type number');
  }
  
  if(!Array.isArray(this)) {
    throw new TypeError("Invalid type")
  }
  
  if(!this.length) {
    return 0
  }
  
  return this.reduce((acc,item)=>acc*=item[position-1],1)
}

function tuple(input) {
  // write your code below

  if (typeof input !== 'string') {
    throw new TypeError('Argument must be of type string');
  }

  // Find all matches following this pattern => (1, 2, 3)
  const groups = [...input.matchAll(DIGITS_ENCLOSED_WITH_BRACKETS_REGEX)];
  
  const res = groups.reduce((acc, item) => {
    // Convert the string match to arrays of numbers
    const arr = item[1].split(',').map(Number);
    acc.push(arr);
    return acc;
  }, []);
  
  res.multiple = multiple;
  
  return res
}

Read-only