Question

I'm trying to understand where i'm going wrong here. say I have an array:

result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]      

I want to get rid of every 6 entries in my array, leave the 7th there, and go one. In effect i'm hoping to get rid of 6/7th of the values in my array. I'm thinking splice() could be perfect for that, but somehow the code below doesn't seem to work. Any suggestions?

Many thanks to you all!

function random_select ()
    {
      for(var i=0; i < result.length; i+7)
      {
        result.splice[i,6];

      }
    };
Was it helpful?

Solution

Try this instead:

var result = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16];
for (var i = 0; i < result.length; i++) result.splice(i, 6);

This will:

  1. First remove [0, 1, 2, 3, 4, 5], and let [6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] be.
  2. Next remove [7, 8, 9, 10, 11, 12] and let [13, 14, 15, 16] be.
  3. Finally remove [13, 14, 15, 16].

Hence the result is [6, 13]. Is this what you want?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top