Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25403
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2014-07-16 07:58 -0700 |
| References | <314cbe9e-975c-4bb1-9e95-b2c90eca3388@googlegroups.com> <e71a7d67-0ecc-456c-8510-0a8a0152825c@googlegroups.com> |
| Message-ID | <6c3f5661-ffb5-4d36-ab28-e6b4efed5bfc@googlegroups.com> (permalink) |
| Subject | Re: General solution to get the previous value |
| From | jonas.thornvall@gmail.com |
Den onsdagen den 16:e juli 2014 kl. 16:23:41 UTC+2 skrev Scott Sauyet:
> jonas.thornvall@gmail.com wrote:
>
> > number=29;
>
> > basemultip=1;
>
> > base=3;
>
> >
>
> > while (basemultip < number)
>
> > {
>
> > prevmultip=basemultip;
>
> > basemultip = basemultip * base;
>
> >
>
> > }
>
> > basemultip=prevbase;
>
> >
>
> > What is the solution to have the previous value when you walk
>
> > out from loop, i think my code look strange and i probably
>
> > learned how to do this but forgot about it.
>
> >
>
> > Can i do it without buffering the previous value?
>
>
>
> Doing this in a function might help. One approach:
>
>
>
> var findLast = function(base, number) {
>
> var current = 1;
>
> while (true) {
>
> next = base * current;
>
> if (next >= number) {return current;}
>
> current = next;
>
> }
>
> }
>
>
>
> findLast(3, 29); //=> 27
>
>
>
> Note that this has no error checking. You might want to check that
>
> your target is greater than 1, and that your base is greater than 1.
>
>
>
> But note that this could easily be abstracted into a more general
>
> pattern, which might itself be useful:
>
>
>
> var findLast = function(start, getNext, predicate) {
>
> var current = start;
>
> while (true) {
>
> next = getNext(current);
>
> if (!predicate(next)) {return current;}
>
> current = next;
>
> }
>
> }
>
>
>
> var base = 3, number = 29;
>
> findLast(1, function(n) {return base * n;},
>
> function(n) {return n < number;}); //=> 27
>
>
>
> Again this is missing some error checking. You might want to at
>
> least test that the initial value passes the predicate:
>
>
>
> if (!predicate(start)) {throw new Error('Illegal initial value');}
>
>
>
> It would be harder in this general case though to ensure that we'll ever hit the termination condition.
>
>
>
> -- Scott
It seem to use two conditional checks, While(true) and if(predicate) but maybe it still faster by not buffering the value each loop?
I think it kind of weird that there seem to be no loop construction that could catch this case.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
General solution to get the previous value jonas.thornvall@gmail.com - 2014-07-15 17:25 -0700
Re: General solution to get the previous value jonas.thornvall@gmail.com - 2014-07-15 21:25 -0700
Re: General solution to get the previous value Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-07-16 13:06 +0100
Re: General solution to get the previous value Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-16 07:23 -0700
Re: General solution to get the previous value jonas.thornvall@gmail.com - 2014-07-16 07:58 -0700
Re: General solution to get the previous value jonas.thornvall@gmail.com - 2014-07-16 13:35 -0700
Re: General solution to get the previous value Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-07-16 17:05 +0100
Re: General solution to get the previous value jonas.thornvall@gmail.com - 2014-07-16 11:07 -0700
Re: General solution to get the previous value Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-16 12:13 -0700
Re: General solution to get the previous value "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-07-16 22:45 +0200
csiph-web