Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Created October 27, 2020 15:07
Show Gist options
  • Select an option

  • Save SteGriff/9d83a9a56579696d630e901dad28b126 to your computer and use it in GitHub Desktop.

Select an option

Save SteGriff/9d83a9a56579696d630e901dad28b126 to your computer and use it in GitHub Desktop.
Generate a sequence which skips numbers and doesn't repeat, like an offset sawtooth wave. Or odd numbers up to n, followed by even numbers up to n.
const skippingIterator = (x, skip, limit) =>
{
return 1 + ((skip * x) % limit) + (Math.floor((skip * x)/limit));
}
const limit = 36;
for (let i = 0; i < limit; i++)
{
console.log(i, skippingIterator(i, 3, limit));
}
// Output:
// x y
// 0 1
// 1 4
// 2 7
// 3 10
// 4 13
// 5 16
// 6 19
// 7 22
// 8 25
// 9 28
// 10 31
// 11 34
// 12 2
// 13 5
// 14 8
// 15 11
// 16 14
// 17 17
// 18 20
// 19 23
// 20 26
// 21 29
// 22 32
// 23 35
// 24 3
// 25 6
// 26 9
// 27 12
// 28 15
// 29 18
// 30 21
// 31 24
// 32 27
// 33 30
// 34 33
// 35 36
@SteGriff
Copy link
Copy Markdown
Author

skipping-iterator-sawtooth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment