Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function recurCheck (arr, index) {
- let flag = true;
- let localIndex = index;
- for (i = localIndex; flag === true;) {
- if (arr[localIndex + 1] === arr[index]) {
- localIndex++
- }
- else {
- flag = false;
- }
- }
- return {start: index, stop: localIndex}
- }
- function arrStartStop (arr) {
- let checkForNestedArr = arr.filter(item => Array.isArray(item));
- if (checkForNestedArr.length) {
- return "nested array"
- }
- let newArr = [];
- for (i = 0; i < arr.length; i++) {
- newArr.push(recurCheck(arr, i));
- }
- return newArr.map((obj, index) => {
- if (obj.start !== obj.stop) {
- let localArr = [];
- for (i = 0; i < (obj.stop - obj.start + 1); i++) {
- localArr.push(arr[obj.stop]);
- }
- return localArr;
- }
- else {
- if (obj.stop !== newArr[index - 1].stop) {
- return [arr[obj.stop]];
- }
- }
- })
- .filter(item => item !== undefined)
- }
- arrStartStop([1, 1, 2, 1, 1, 1, 3, 3])
- arrStartStop(["a", "a", "b", "b", "c"])
- arrStartStop([[1, 2], [1, 2], [3, 4]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement