Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getSeries = function(series, groupSize) {
- // checking if the values provided match the description
- if (series == null) return 'provide a string';
- if (groupSize == null) return 'provide size';
- if (Number.isInteger(groupSize) == false)
- return 'provide a whole number for size';
- if (groupSize == 0) return 'size cannot be 0';
- // creating arrays
- const arrayOfNumbers = [];
- const arrayOfNumericArrays = [];
- // spliting the string provided into groups
- const arrayOfStrings = series.match(new RegExp('.{' + groupSize + '}', 'g'));
- arrayOfStrings.forEach(element => {
- arrayOfNumbers.push(parseInt(element));
- });
- // checking if there is a part of the string thats left ungrouped
- if ((series.length / groupSize) % 1 != 0)
- arrayOfNumbers.push(parseInt(series.slice(-groupSize)));
- // transforming the result into the desired format
- arrayOfNumbers.forEach(element => {
- const digits = element.toString().split('');
- const realDigits = digits.map(Number);
- arrayOfNumericArrays.push(realDigits);
- });
- return arrayOfNumericArrays;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement