[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.javascript

Set flag/boolean when loop finish?

JT

7/17/2014 5:29:00 PM

This code seem unecessarily set the flag to one multiple times, how can i condition the while to just set it as it step out of loop?

while (i * basemultip <= number && i < bas)
{
j = i;
i ++ ;
set = 1;
}
6 Answers

JT

7/17/2014 5:39:00 PM

0

Den torsdagen den 17:e juli 2014 kl. 19:29:23 UTC+2 skrev jonas.t...@gmail.com:
> This code seem unecessarily set the flag to one multiple times, how can i condition the while to just set it as it step out of loop?
>
>
>
> while (i * basemultip <= number && i < bas)
>
> {
>
> j = i;
>
> i ++ ;
>
> set = 1;
>
> }

Managed to reduce some conditional checks, well they was unnecessary to start with.

function enctwo(number, bas)
{
basestr = "";
basemultip = 1;
digit = 1;

while (basemultip < number)
{
basemultip = basemultip * bas;
digit = digit + 1;
}

while (number > 0 && digit > 0)
{
set = 0;
i = 0;
while (i * basemultip <= number && i < bas)
{
j = i;
i ++ ;
set = 1;
}
if (set == 1)
{
subtrahend = basemultip * j;
number = number - subtrahend;
basestr = basestr + j + " ";
}
//else if (set == 0 && number != 0)basestr = basestr + 0 + " ";
else {basestr = basestr + 0 + " ";}
basemultip = basemultip / bas;
digit -- ;
}
while (digit > 0)
{
basestr = basestr + 0 + " ";
digit -- ;
}
return basestr;
}

Ben Bacarisse

7/17/2014 6:06:00 PM

0

jonas.thornvall@gmail.com writes:
<snip>
> Managed to reduce some conditional checks, well they was unnecessary
> to start with.
<snip>
> set = 0;
> i = 0;
> while (i * basemultip <= number && i < bas)
> {
> j = i;
> i ++ ;
> set = 1;
> }
> if (set == 1)

Here, the conditions set == 1 and i > 0 are the same. There's no need
for 'set'.

<snip>
--
Ben.

JT

7/17/2014 6:19:00 PM

0

Den torsdagen den 17:e juli 2014 kl. 20:06:23 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> <snip>
>
> > Managed to reduce some conditional checks, well they was unnecessary
>
> > to start with.
>
> <snip>
>
> > set = 0;
>
> > i = 0;
>
> > while (i * basemultip <= number && i < bas)
>
> > {
>
> > j = i;
>
> > i ++ ;
>
> > set = 1;
>
> > }
>
> > if (set == 1)
>
>
>
> Here, the conditions set == 1 and i > 0 are the same. There's no need
>
> for 'set'.
>
>
>
> <snip>
>
> --
>
> Ben.

Of course, thanks Ben.

JT

7/17/2014 6:29:00 PM

0

Den torsdagen den 17:e juli 2014 kl. 20:06:23 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> <snip>
>
> > Managed to reduce some conditional checks, well they was unnecessary
>
> > to start with.
>
> <snip>
>
> > set = 0;
>
> > i = 0;
>
> > while (i * basemultip <= number && i < bas)
>
> > {
>
> > j = i;
>
> > i ++ ;
>
> > set = 1;
>
> > }
>
> > if (set == 1)
>
>
>
> Here, the conditions set == 1 and i > 0 are the same. There's no need
>
> for 'set'.
>
>
>
> <snip>
>
> --
>
> Ben.

Would it be possible to go direct to bijective code from this by reduce the previous digit when a zero appear and make next digit base, and if another 0 follow reduce that base value-1, sort of roll it off?

JT

7/17/2014 6:41:00 PM

0

Den torsdagen den 17:e juli 2014 kl. 20:28:50 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdagen den 17:e juli 2014 kl. 20:06:23 UTC+2 skrev Ben Bacarisse:
>
> > jonas.thornvall@gmail.com writes:
>
> >
>
> > <snip>
>
> >
>
> > > Managed to reduce some conditional checks, well they was unnecessary
>
> >
>
> > > to start with.
>
> >
>
> > <snip>
>
> >
>
> > > set = 0;
>
> >
>
> > > i = 0;
>
> >
>
> > > while (i * basemultip <= number && i < bas)
>
> >
>
> > > {
>
> >
>
> > > j = i;
>
> >
>
> > > i ++ ;
>
> >
>
> > > set = 1;
>
> >
>
> > > }
>
> >
>
> > > if (set == 1)
>
> >
>
> >
>
> >
>
> > Here, the conditions set == 1 and i > 0 are the same. There's no need
>
> >
>
> > for 'set'.
>
> >
>
> >
>
> >
>
> > <snip>
>
> >
>
> > --
>
> >
>
> > Ben.
>
>
>
> Would it be possible to go direct to bijective code from this by reduce the previous digit when a zero appear and make next digit base, and if another 0 follow reduce that base value-1, sort of roll it off?

The nasty case would be when the leading number of the zeros is 1 i guess.

JT

7/17/2014 6:46:00 PM

0

Den torsdagen den 17:e juli 2014 kl. 20:40:36 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdagen den 17:e juli 2014 kl. 20:28:50 UTC+2 skrev jonas.t...@gmail.com:
>
> > Den torsdagen den 17:e juli 2014 kl. 20:06:23 UTC+2 skrev Ben Bacarisse:
>
> >
>
> > > jonas.thornvall@gmail.com writes:
>
> >
>
> > >
>
> >
>
> > > <snip>
>
> >
>
> > >
>
> >
>
> > > > Managed to reduce some conditional checks, well they was unnecessary
>
> >
>
> > >
>
> >
>
> > > > to start with.
>
> >
>
> > >
>
> >
>
> > > <snip>
>
> >
>
> > >
>
> >
>
> > > > set = 0;
>
> >
>
> > >
>
> >
>
> > > > i = 0;
>
> >
>
> > >
>
> >
>
> > > > while (i * basemultip <= number && i < bas)
>
> >
>
> > >
>
> >
>
> > > > {
>
> >
>
> > >
>
> >
>
> > > > j = i;
>
> >
>
> > >
>
> >
>
> > > > i ++ ;
>
> >
>
> > >
>
> >
>
> > > > set = 1;
>
> >
>
> > >
>
> >
>
> > > > }
>
> >
>
> > >
>
> >
>
> > > > if (set == 1)
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > > Here, the conditions set == 1 and i > 0 are the same. There's no need
>
> >
>
> > >
>
> >
>
> > > for 'set'.
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > >
>
> >
>
> > > <snip>
>
> >
>
> > >
>
> >
>
> > > --
>
> >
>
> > >
>
> >
>
> > > Ben.
>
> >
>
> >
>
> >
>
> > Would it be possible to go direct to bijective code from this by reduce the previous digit when a zero appear and make next digit base, and if another 0 follow reduce that base value-1, sort of roll it off?
>
>
>
> The nasty case would be when the leading number of the zeros is 1 i guess.

And an array approach rather then a string would be necessary.