Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #25223
| Newsgroups | comp.lang.javascript |
|---|---|
| Date | 2014-07-05 12:12 -0700 |
| References | (6 earlier) <de2372d0-0c2a-4ef5-b1b4-547ff7a154a1@googlegroups.com> <ce1ff707-717d-43d6-b2b5-70fa7e8c560d@googlegroups.com> <841ac7ab-d05e-43ea-9b43-65c99ca623b9@googlegroups.com> <00c43bf0-1dcd-45f6-ab25-6662e770b0ce@googlegroups.com> <a0a98cd4-2b15-4c71-8108-a3ab66705f65@googlegroups.com> |
| Message-ID | <14887dfc-6279-4e80-aee9-da37c50845b4@googlegroups.com> (permalink) |
| Subject | Re: Bijective basechanger |
| From | Scott Sauyet <scott.sauyet@gmail.com> |
jonas.thornvall@gmail.com wrote:
> skrev Scott Sauyet:
>> Here is my approach:
>>
>> var zeroless = function(nbr, base) {
>> if (!nbr || nbr === 0) {return "";}
>> if (!base) {base = 10;}
>> var str = "" + nbr.toString(base);
>> var lastChar = str.slice(-1);
>> var beginning = str.slice(0, -1);
>> return lastChar === "0" ?
>> zeroless(parseInt(beginning, base) - 1, base) +
>> (base).toString(base + 1).toUpperCase() :
>> zeroless(parseInt(beginning, base), base) + lastChar;
>> };
>
> It works Scott, i checked binary and ternary
I'm glad it works. But this really wasn't a big leap from
the earlier version I posted that worked only in base 10.
> i must say every time i look at recursive function calls
> it is really hard for me to follow the code, but i probably
> should learn howto code it.
Recursion should generally be IMHO *easier* to understand.
It's the process of breaking your problem down into smaller
sub-problems and solving those.
This solution works (in base 10) by splitting the digits in
two parts: the final digit and the set up to the final digit.
If the final digit is not zero 0, then we're in the easy case,
and all we do is process the remaining digits and append that
final digit to that result. If that final digit is a 0, then
we have to think of it as a 10, which means we have to subtract
one from the remaining digits before processing them and
appending the "A" for 10. The only thing left is to work out
the base of the recursion, which turns out to be pretty easy,
because we can just check if the number is zero.
> I will try figure out why my while loop approach fail.
That's probably a good idea.
> Thanks again Scott
You're welcome. You could express your gratitude well by
learning to quote appropriately on USENET. Your posts are
very hard to read.
One final question. Is this actually useful for something?
Or is it mostly a puzzle?
-- Scott
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 06:42 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-17 12:08 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 13:23 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 13:24 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-17 13:54 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 14:07 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 14:15 -0700
Re: Bijective basechanger John Harris <niam@jghnorth.org.uk.invalid> - 2014-06-23 17:23 +0100
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 14:23 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-17 14:41 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-17 20:28 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 13:04 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-18 16:30 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 17:45 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 17:49 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-19 11:21 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-19 14:44 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-19 14:48 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-19 14:55 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-19 19:59 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-21 09:25 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-21 11:15 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 18:05 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 18:19 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 18:25 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-18 18:46 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-23 16:00 -0700
Re: Bijective basechanger "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2014-06-23 17:51 -0700
Re: Bijective basechanger John Harris <niam@jghnorth.org.uk.invalid> - 2014-06-23 17:18 +0100
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-23 11:27 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-23 15:29 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 03:26 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 03:28 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 04:24 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 05:36 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-24 13:05 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 14:13 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-25 05:36 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-25 12:45 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-25 13:15 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-01 02:39 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-03 07:33 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-03 09:54 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-04 02:14 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-05 12:12 -0700
Re: Bijective basechanger "Evertjan." <exxjxw.hannivoort@inter.nl.net> - 2014-07-05 22:19 +0200
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-05 13:50 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-06 11:11 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-06 14:59 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-06 15:00 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-06 15:02 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-16 23:31 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-07-04 02:28 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-07-05 12:14 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-25 22:38 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 03:32 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 03:44 -0700
Re: Bijective basechanger jonas.thornvall@gmail.com - 2014-06-24 11:10 -0700
Re: Bijective basechanger Scott Sauyet <scott.sauyet@gmail.com> - 2014-06-24 11:45 -0700
Re: Bijective basechanger "Chris M. Thomasson" <no@spam.invalid> - 2014-07-22 12:16 -0700
csiph-web