That only gives you even or odd increments to the top number...

WHat if you wanted every 5th number ...

Better to go with my/TJ's loop


Alex Mcauley
http://www.thevacancymarket.com
----- Original Message ----- 
From: "Matt Foster" <mattfoste...@gmail.com>
To: "Prototype & script.aculo.us" <prototype-scriptaculous@googlegroups.com>
Sent: Friday, September 25, 2009 5:57 PM
Subject: [Proto-Scripty] Re: Range utility increment



I'd just use an 'iterator' class to do this...

var EvenNumber = Class.create(
{
initialize : function(num){
this.num = (num % 2 == 1) ? num - 1 : num;
},
succ : function(){
return new EvenNumber(this.num + 2);
},
toString : function(){
return this.num;
}
});

var bottom = new EvenNumber(1);

var top = new EvenNumber(21);
var range = $A($R(bottom, top));

console.log(range);

I had done this to a much greater extent in my date range selector
gadget.  Where it became the master range, held a range of calendar
objects, which were in themselves ranges of date objects... fun stuff.

https://positionabsolute.net/blog/2008/01/google-calendar-date-range-selection.php

ps just looking over that, should rename that class to
"PositiveEvenNumber", only going to work one way, but you get the idea


On Sep 25, 4:15 am, "Alex McAuley" <webmas...@thecarmarketplace.com>
wrote:
> function everyOther(start, end, increment) {
> var n;
> var rv = [];
> for (n = start; n <= end; n += increment) {
> rv[rv.length] = n;
> }
> return rv;
> }
>
> Just a mod to make it better to be able to change it to 3's, 4's, 5's
>
> ;)
>
> Alex Mcauleyhttp://www.thevacancymarket.com
>
> ----- Original Message -----
> From: "T.J. Crowder" <t...@crowdersoftware.com>
> To: "Prototype & script.aculo.us" 
> <prototype-scriptaculous@googlegroups.com>
> Sent: Friday, September 25, 2009 8:29 AM
> Subject: [Proto-Scripty] Re: Range utility increment
>
> Hi,
>
> > I'm looking for a way to specify that I want to increment by any
> > value. Let's say count up by 2's: [2,4,6,8,10]. How do you do this?
>
> ObjectRange itself is for ranges of consecutive values, but you can
> get an array of such values by applying Enumerable#collect to a range:
>
> var twos;
> twos = $R(1, 5).collect(function(x) { return x * 2; });
>
> ...although frankly I'd probably go with a straight loop like Alex did
> (although a slightly different one):
>
> function everyOther(start, end) {
> var n;
> var rv = [];
> for (n = start; n <= end; n += 2) {
> rv[rv.length] = n;
> }
> return rv;
> }
>
> FWIW,
> --
> T.J. Crowder
> tj / crowder software / comwww.crowdersoftware.com
>
> On Sep 24, 11:15 pm, JoJo <tokyot...@gmail.com> wrote:
> >http://www.prototypejs.org/api/utility/dollar-r
>
> > From the documentation of the Range utility, it seems like it can only
> > increment by 1's. For example, $A($R(1,10,true)) gives you:
> > [1,2,3,4,5,6,7,8,9,10].
>
> > I'm looking for a way to specify that I want to increment by any
> > value. Let's say count up by 2's: [2,4,6,8,10]. How do you do this?



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to