const DIGITS_ENCLOSED_WITH_BRACKETS_REGEX =
/\(\s*(\d+\s*(?:,\s*\d+\s*)*)\s*\)/g;
function multiple(position) {
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) {
if (typeof input !== 'string') {
throw new TypeError('Argument must be of type string');
}
const groups = [...input.matchAll(DIGITS_ENCLOSED_WITH_BRACKETS_REGEX)];
const res = groups.reduce((acc, item) => {
const arr = item[1].split(',').map(Number);
acc.push(arr);
return acc;
}, []);
res.multiple = multiple;
return res
}
Read-only