-3

I made a JavaScript loop that skips every set of two numbers that are followed by a set of three.

Consider the following set of integers:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10

This is what the array returns:

1, 2, 3, 6, 7, 8

See how it skipped 4-5 and 9-10? (every set of two numbers followed by a set of three)

Visual:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10 (skips bolded numbers)

Here is what I came up with:

var y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    for (var i = 0; i < y.length; i++) {
        if (y[i] >= 4 && y[i] <= 5) {
            continue;
        }
        if (y[i] >= 9 && y[i] <= 10) {
            continue;
        }
        document.write(y[i]);
    }

The above for-loop returns 123678 from 12345678910. The problem is that I had to manually say which ones to skip. Is there a way, possibly with % modulus, to automatically skip every set of two numbers followed by a set of three without limiting the length to ten?

Community
  • 1
  • 1
Matthew
  • 2,158
  • 7
  • 30
  • 52
  • "I made a JavaScript loop that skips every set of two numbers that are followed by a set of three." - your code does something quite different. please clarify, what's the task? – Karoly Horvath Jul 07 '14 at 00:36
  • Yeah, no. This does EXACTLY what I said it does. – Matthew Jul 07 '14 at 00:38
  • 1
    Obviously not, that's why I'm asking. – Karoly Horvath Jul 07 '14 at 00:39
  • 1
    I think what @KarolyHorvath is referring to is that you're describing a pattern of iteration, but implementing a pattern based on value exclusion. It's like saying I've invented a time machine that takes you forward in time exactly 5 minutes. Just step into the machine and then come out in 5 minutes, and you'll be in the future. Technically true, but doesn't really live up to the description. – cookie monster Jul 07 '14 at 00:45
  • 1
    @cookiemonster: that's an impressive analogy. also worth mentioning, that 9 10 is skipped in the example even though they aren't followed by anything. – Karoly Horvath Jul 07 '14 at 00:46
  • @KarolyHorvath: Good point. You're right, the result doesn't match the description. – cookie monster Jul 07 '14 at 00:49
  • @KarolyHorvath 9-10 are followed by 11, 12, and so on. I just limited this to 10 for everyone's sake. My for-loop was only limited to ten, so I came here looking for an adequate answer for an infinite amount of numbers – Matthew Jul 07 '14 at 00:56
  • try this one :) `for(var i = 0, l = y.length; i < l; i++) { for(var j = i; j < i + 3 && j < l ; j ++) document.write(y[i]); i += 4; }` – أنيس بوهاشم Jul 07 '14 at 01:18

6 Answers6

6

The pattern for the processing repeats after 5 items, so yes, you can use the modulo operator:

if (i % 5 < 3)
    document.write(y[i]);
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • 2
    This, without explanation, after criticising adeneo for being "[unnecessarily complex...not immediately obvious](http://stackoverflow.com/questions/24601553/javascript-for-loop-skip-2-numbers-followed-by-a-set-of-3#comment38117755_24601610)"? – David Thomas Jul 07 '14 at 00:46
  • @David Thomas: to be honest - it is simple. It is indeed simpler than a loop, 2 checks and 2 counters. – zerkms Jul 07 '14 at 00:47
  • @DavidThomas: Shorter, no extraneous variables -- looks like a winner to me. – Scott Hunter Jul 07 '14 at 00:47
  • Well, at least you posted an answer using modulus, so +1 to you. However, if you couldn't understand my answer and thought it wasn't immediately obvious was going on there, it's no wonder it took you fifteen minutes to figure that one out. – adeneo Jul 07 '14 at 00:49
  • It wasn't so much the complexity I was referring to, but mostly the "not immediately obvious" part, I could skim adeneo's and understand, this took a pause to read. Obviously ymmv, and so forth. – David Thomas Jul 07 '14 at 00:49
  • @David Thomas: "I could skim adeneo's and understand" --- are you serious now? It is unlikely anyone could restore an original task having only code. Try it on any of your colleagues. – zerkms Jul 07 '14 at 00:50
  • @David Thomas: I don't believe it. Try it on a someone who doesn't know the original task. – zerkms Jul 07 '14 at 00:51
  • @zerkms - why wouldn't he understand it, it's really easy to understand and took me a couple of minutes to figure out and write ? – adeneo Jul 07 '14 at 00:53
  • @adeneo: I said it wasn't immediately obvious, because TBH, it wasn't. that, of course, doesn't mean I couldn't figure out. And it took me 2 seconds after reading the question. I just don't compete in this who's posting first race... i just didn't like the look of that checkmark next to the other answer. – Karoly Horvath Jul 07 '14 at 00:53
  • @adeneo: honestly, I wouldn't understand your intentions if I didn't know the original task and just saw your code. This code wouldn't pass my code review as not enough expressive. Indeed it's 100% subjective but I think that [my solution](http://stackoverflow.com/a/24601692/251311) is much more readable – zerkms Jul 07 '14 at 00:54
  • @David Thomas & adeneo: http://chat.stackoverflow.com/transcript/message/17551376#17551376 -- here are impressions of 2 random guys – zerkms Jul 07 '14 at 00:57
  • @zerkms: I...don't know how to respond. My opinion is wrong because two 'random guys' have a contrary opinion? What are you trying to prove? Read my comment again (I edited after you edited yours, which clarified your question). – David Thomas Jul 07 '14 at 01:02
  • 1
    @zerkms - I don't really care, I posted an answer with code that seemed obvious to me, but this answer is obviously better as it's getting more upvotes from the community, while my fully working answer is getting downvoted. As for reability, yours is better than mine, and this one sucks in my opinion, but it's clever, more so than yours and mine. I actually care so little that if I could I would delete my answer and just move along to the next one. – adeneo Jul 07 '14 at 01:02
2

after seeing @ysf comment i have to explain what i did for anyone who is seeing this answer - sorry for bad English if exists -

first of all you need to analyze things, what do we want? base on this everything will be solved nice and clear

so we have an array and you want to pick numbers and skips other right? in order to achieve this and to know we will use the mod operator "%"

if we want to pick 2 numbers and skip three numbers, pick 2 skip 3 ...etc) we first sum the pick + skip values in order to make it on the right hand of the mod operation ... in our case 2 + 3 = 5

let all = pick + skip;

in order to pick from array i don't need the value of the index ... i need the index it self pick index[0] and index[1] skip index 2,3,4

because i want to pick only 2, then our expression will be limited to only 2 and to the sum of the pick and skip values

[index] % all < pick so if the index mod all is less than the limit which is 2 then take it otherwise leave (skip) it

so the results of our loop will look like this:

i % all < pick ===> 0 % 5 < 2 = true
i % all < pick ===> 1 % 5 < 2 = true
i % all < pick ===> 2 % 5 < 2 = false
i % all < pick ===> 3 % 5 < 2 = false
i % all < pick ===> 4 % 5 < 2 = false
i % all < pick ===> 5 % 5 < 2 = true
i % all < pick ===> 6 % 5 < 2 = true
i % all < pick ===> 7 % 5 < 2 = false
i % all < pick ===> 8 % 5 < 2 = false
i % all < pick ===> 9 % 5 < 2 = false
i % all < pick ===> 10 % 5 < 2 = true
i % all < pick ===> 11 % 5 < 2 = true

and the last thing we use filter because this is a JS function that returns an Array

const skip = (arr, pick, skip) => {
  let all = pick + skip;
  let filtered = arr.filter((_, i) => {
    return i % all < pick;
  });
  console.log(filtered);
};

}

skip([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],2,3);
Zaid abu khalaf
  • 776
  • 8
  • 15
  • 1
    While this code may resolve the OP's issue, it is best to include an explanation as to how your code addresses the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can edit to add additional info &/or to supplement your explanations with source documentation – ysf Jun 09 '20 at 21:34
1

Here is JSFiddle link

var seq = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
  pick = 3,
  skip = 2;

var r = skipFilter(seq, pick, skip);
console.log(r);

function skipFilter(seq, pick, skip) {
  var stride = pick + skip;

  return seq.filter(function(_, i) {
    return i % stride < pick;
  });
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
zerkms
  • 249,484
  • 69
  • 436
  • 539
0
for (var i=0; i < y.length; i++) {

  if (i % 5 < 3) {
    document.write(y[i] + ' ');
  } else {
    i += 1;
    document.write('<br>');
  }
}
-1

You could just keep a counter instead

for (var i=0, j=0; i < y.length; i++) {

    if (j < 3) {
        document.write(y[i]);
    } else if ( j > 3) {
        j = -1;
    }

    j++;
}

FIDDLE

adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Why all the thumbs down?? This is right. Good job, thank you! – Matthew Jul 07 '14 at 00:33
  • 1
    @Matthew - I guess someone didn't get it, but you can't really use a modulus to get a set of three, then skip two etc. or at least it would be too complicated, just keeping a counter that is reset is the way to do this. – adeneo Jul 07 '14 at 00:34
  • unncessary complex. it's not immediately obvious from the code what it does. – Karoly Horvath Jul 07 '14 at 00:35
  • 1
    @KarolyHorvath - Then post an answer – adeneo Jul 07 '14 at 00:35
  • 2
    @KarolyHorvath - If you think this is complex, post an answer that uses modulus that is simpler. It's really obvious what this does, if `j` is less than 3, then write from the array, if `j` is more than 3 reset `j` to start over again. – adeneo Jul 07 '14 at 00:39
  • 1
    @adeneo: I don't think it's complex. I said it's *unnecessary* complex. that's a comparison (more complex than...) – Karoly Horvath Jul 07 '14 at 00:55
-1

Move the "i" increment to the body of "for"

    var y = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

    for (var i=0; i<y.length;) {
        console.log(y[i]);
        console.log(y[i+1]);
        console.log(y[i+2]);

        i = i + 5;
    }
seaBass
  • 585
  • 1
  • 6
  • 17