Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.09 KB | None | 0 0
  1. const getSeries = function(series, groupSize) {
  2. // checking if the values provided match the description
  3. if (series == null) return 'provide a string';
  4. if (groupSize == null) return 'provide size';
  5. if (Number.isInteger(groupSize) == false)
  6. return 'provide a whole number for size';
  7. if (groupSize == 0) return 'size cannot be 0';
  8.  
  9. // creating arrays
  10. const arrayOfNumbers = [];
  11. const arrayOfNumericArrays = [];
  12.  
  13. // spliting the string provided into groups
  14. const arrayOfStrings = series.match(new RegExp('.{' + groupSize + '}', 'g'));
  15. arrayOfStrings.forEach(element => {
  16. arrayOfNumbers.push(parseInt(element));
  17. });
  18.  
  19. // checking if there is a part of the string thats left ungrouped
  20. if ((series.length / groupSize) % 1 != 0)
  21. arrayOfNumbers.push(parseInt(series.slice(-groupSize)));
  22.  
  23. // transforming the result into the desired format
  24. arrayOfNumbers.forEach(element => {
  25. const digits = element.toString().split('');
  26. const realDigits = digits.map(Number);
  27. arrayOfNumericArrays.push(realDigits);
  28. });
  29.  
  30. return arrayOfNumericArrays;
  31. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement