0

I'm trying to create subsets of an Array in a recurring manner. I've an Array in the form of [0,1,2,3,4,5,6]. I'd like to create subsets like [0,1,2], [1,2,3],[2,3,4],[3,4,5],[4,5,6],[5,6,0],[6,0,1],[0,1,2] and so on. But, its not breaking after [4,5,6].

Here is my code

var app = angular.module('app',[]);
app.controller('EventController',function EventController($scope) {
   $scope.count = 0;
   var arr = [], subArr = [], currentIndex;  
   for(var i = 0; i < 7; i++) {
      arr.push(i)
   }
   $scope.next = function() {
   currentIndex = $scope.count;  
   subArr = [];
   $scope.count++;    
   subArr.push(currentIndex);
   subArr.push(currentIndex+1);
   subArr.push(currentIndex+2);
   if(arr.indexOf(currentIndex+1) == -1) {
     console.log(currentIndex+1,'Element not available');
     $scope.count = 0;
     currentIndex = $scope.count
   }
   console.log(subArr);
 } 
});

Here is the fiddle

Any ideas would be greatly appreciated.

3 Answers 3

2
Try this:

var app = angular.module('app',[]);
app.controller('EventController',function EventController($scope) {
  $scope.count = 0;
  var  subArr = [], currentIndex; 
  $scope.arr = [];
  for(var i = 0; i < 7; i++) {
    $scope.arr.push(i);
  }

  $scope.next = function() {
    currentIndex = $scope.count;
    //console.log(currentIndex);
    subArr = [];
    $scope.count++;    
    subArr.push(currentIndex);  
    if($scope.arr.indexOf(currentIndex) == $scope.arr.length -2) {
    console.log(currentIndex+2,'Element not available');
       subArr.push(currentIndex+1);
        $scope.count = 0;
     subArr.push($scope.arr[0]);

    }
    else if($scope.arr.indexOf(currentIndex) == $scope.arr.length -1) {
    console.log(currentIndex+1,'Element not available');
     subArr.push($scope.arr[0]);
      subArr.push($scope.arr[1]);
      currentIndex = $scope.count;
    }
        else{
    subArr.push(currentIndex+1);
    subArr.push(currentIndex+2);
    }
    console.log(subArr);
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

The problem is, you are pushing elements before if condition. 1. Move the if block before pushing data to array 2. Change arr.indexOf(currentIndex+1) == -1 to arr.indexOf(currentIndex+2) == -1

var app = angular.module('app',[]);
app.controller('EventController',function EventController($scope) {
  $scope.count = 0;
  var arr = [], subArr = [], currentIndex;  
  for(var i = 0; i < 7; i++) {
    arr.push(i)
  }
  $scope.next = function() {
    currentIndex = $scope.count;
    //console.log(currentIndex);
    subArr = [];
    $scope.count++;

    subArr.push((currentIndex)%arr.length);
    subArr.push((currentIndex+1)%arr.length);
    subArr.push((currentIndex+2)%arr.length);

    console.log(subArr);
  }
});

8 Comments

Amit, partially its right. But not totally, if you see logs, you can see after [4, 5, 6] [0, 1, 2] is coming, but [5,6,0] should come.
Thanks for the effort. Please check the logs. It should recursively get the subsets
but your count will always increment. Is that fine?
Its giving me the correct output. Increment I didn't get you.?
My event count shows 7,8,9,10 earlier it was ,5,6,0,1 i guess
|
0

I guess the below code will help. It is self explanatory.

function createSubset (array, subsetSize, subsets){

  var inputArray = array,
  subset,
  subsetCollection = [],
  noOfSubsets = subsets || inputArray.length + 1,
  size = subsetSize,
  current = 0,
  i;

  while (noOfSubsets--) {    
   subset = [];
   for (i = 0; i < size; i++) {
    subset.push( inputArray[ (current + i) % inputArray.length] );
   }
   current += 1 ;
   subsetCollection.push(subset);
  }

  return subsetCollection;
}

console.log('Test Case start');
var a = [0,1,2,3,4,5,6], 
result = createSubset(a, 3);
console.log(result);
console.log('Test Case end'); 


console.log('Test Case start');
var a = [0,1,2,3,4,5,6], 
result = createSubset(a, 3, 4);
console.log(result);
console.log('Test Case end');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.