[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Swap arrays ones again.

JT

5/14/2015 1:56:00 PM

I have this problem with feeding the function in correct order.

This function work great to swap the arrays as long they really are One and Two

->checkArrayOrder(One,Two) works fine and will swap the arrays if arrCompare return true.


function checkArrayOrder(One,Two)
{
res = arrCompare(One, Two) //Here i check which one bigger smaller
if (res)
{
document.calc.stats.value += "Swap arrays! First array operand smaller=" + res + "\n";
var tempArr = One.slice();
One = [];
One = Two.slice();
Two = [];
Two = tempArr.slice();
}
}

***But*** i want a more general function to swap any *GLOBAL* arrays?

checkArrayOrder(A,B) will not work and swap the arrays A and B.



How can i get the function to do the swap on any arrays it is fed with?
4 Answers

JT

5/14/2015 2:13:00 PM

0

Den torsdag 14 maj 2015 kl. 15:55:47 UTC+2 skrev jonas.t...@gmail.com:
> I have this problem with feeding the function in correct order.
>
> This function work great to swap the arrays as long they really are One and Two
>
> ->checkArrayOrder(One,Two) works fine and will swap the arrays if arrCompare return true.
>
>
> function checkArrayOrder(One,Two)
> {
> res = arrCompare(One, Two) //Here i check which one bigger smaller
> if (res)
> {
> document.calc.stats.value += "Swap arrays! First array operand smaller=" + res + "\n";
> var tempArr = One.slice();
> One = [];
> One = Two.slice();
> Two = [];
> Two = tempArr.slice();
> }
> }
>
> ***But*** i want a more general function to swap any *GLOBAL* arrays?
>
> checkArrayOrder(A,B) will not work and swap the arrays A and B.
>
>
>
> How can i get the function to do the swap on any arrays it is fed with?

How can i make the function work on the actual arrays the function is fed with.
Should i return the array one and two.

And then do slice once again A=One.slice(); B=Two.slice(), that just seems wrong but mayb is the way one do it?

But i can not return two arrays anyway, i am stuck.



Ben Bacarisse

5/14/2015 3:12:00 PM

0

jonas.thornvall@gmail.com writes:

> I have this problem with feeding the function in correct order.

The solution is the pass the arrays in the correct order to being with.
I posted about this the last time you asked:

function doSomething(A, B) {
return lessThan(A, B) ? reallyDoIt(A, B) : reallyDoIt(B, A);
}

> This function work great to swap the arrays as long they really are One and Two
>
> ->checkArrayOrder(One,Two) works fine and will swap the arrays if
> arrCompare return true.

That's a terrible name. A function the determines some property should
be named for the condition it tests. If a function tests for an even
number, call it 'isEven', not 'checkParity'. Call a prime tester
'prime' not 'check Divisors', etc.

But here, the function both determines some condition and the acts on
that condition. It should be called 'swapIfLessThan' (or similar).

> function checkArrayOrder(One,Two)
> {
> res = arrCompare(One, Two) //Here i check which one bigger smaller

If you used a better name, the comment would be redundant, but even with
the comment I have no idea which way the test goes!

<snip>
> ***But*** i want a more general function to swap any *GLOBAL* arrays?

Stop wanting that! Manipulating global objects makes for terrible code.

<snip>
--
Ben.

JT

5/14/2015 5:18:00 PM

0

Den torsdag 14 maj 2015 kl. 17:12:33 UTC+2 skrev Ben Bacarisse:
> jonas.thornvall@gmail.com writes:
>
> > I have this problem with feeding the function in correct order.
>
> The solution is the pass the arrays in the correct order to being with.
> I posted about this the last time you asked:
>
> function doSomething(A, B) {
> return lessThan(A, B) ? reallyDoIt(A, B) : reallyDoIt(B, A);
> }
>
> > This function work great to swap the arrays as long they really are One and Two
> >
> > ->checkArrayOrder(One,Two) works fine and will swap the arrays if
> > arrCompare return true.
>
> That's a terrible name. A function the determines some property should
> be named for the condition it tests. If a function tests for an even
> number, call it 'isEven', not 'checkParity'. Call a prime tester
> 'prime' not 'check Divisors', etc.
>
> But here, the function both determines some condition and the acts on
> that condition. It should be called 'swapIfLessThan' (or similar).
>
> > function checkArrayOrder(One,Two)
> > {
> > res = arrCompare(One, Two) //Here i check which one bigger smaller
>
> If you used a better name, the comment would be redundant, but even with
> the comment I have no idea which way the test goes!
>
> <snip>
> > ***But*** i want a more general function to swap any *GLOBAL* arrays?
>
> Stop wanting that! Manipulating global objects makes for terrible code.
>
> <snip>
> --
> Ben.

Thank you Ben.

When called from main it works fine like this.

checkArrayOrder(arrOne,arrTwo);

function checkArrayOrder(A, B) {
return lessThan(A, B) ? resultmult = naiveMult(base, arrTwo, arrOne) : resultmult = naiveMult(base, arrOne, arrTwo);
}

In this case i have no trouble reach resultmult value.

But when called from within my conversion function the resultmult returned is zero.

resultmult is a global array, it is reset when i step into conversion.
resultmult=[]
and initialised
resultmult[0]=1.



But

JT

5/14/2015 5:48:00 PM

0

Den torsdag 14 maj 2015 kl. 19:18:02 UTC+2 skrev jonas.t...@gmail.com:
> Den torsdag 14 maj 2015 kl. 17:12:33 UTC+2 skrev Ben Bacarisse:
> > jonas.thornvall@gmail.com writes:
> >
> > > I have this problem with feeding the function in correct order.
> >
> > The solution is the pass the arrays in the correct order to being with.
> > I posted about this the last time you asked:
> >
> > function doSomething(A, B) {
> > return lessThan(A, B) ? reallyDoIt(A, B) : reallyDoIt(B, A);
> > }
> >
> > > This function work great to swap the arrays as long they really are One and Two
> > >
> > > ->checkArrayOrder(One,Two) works fine and will swap the arrays if
> > > arrCompare return true.
> >
> > That's a terrible name. A function the determines some property should
> > be named for the condition it tests. If a function tests for an even
> > number, call it 'isEven', not 'checkParity'. Call a prime tester
> > 'prime' not 'check Divisors', etc.
> >
> > But here, the function both determines some condition and the acts on
> > that condition. It should be called 'swapIfLessThan' (or similar).
> >
> > > function checkArrayOrder(One,Two)
> > > {
> > > res = arrCompare(One, Two) //Here i check which one bigger smaller
> >
> > If you used a better name, the comment would be redundant, but even with
> > the comment I have no idea which way the test goes!
> >
> > <snip>
> > > ***But*** i want a more general function to swap any *GLOBAL* arrays?
> >
> > Stop wanting that! Manipulating global objects makes for terrible code.
> >
> > <snip>
> > --
> > Ben.
>
> Thank you Ben.
>
> When called from main it works fine like this.
>
> checkArrayOrder(arrOne,arrTwo);
>
> function checkArrayOrder(A, B) {
> return lessThan(A, B) ? resultmult = naiveMult(base, arrTwo, arrOne) : resultmult = naiveMult(base, arrOne, arrTwo);
> }
>
> In this case i have no trouble reach resultmult value.
>
> But when called from within my conversion function the resultmult returned is zero.
>
> resultmult is a global array, it is reset when i step into conversion.
> resultmult=[]
> and initialised
> resultmult[0]=1.
>
>
>
> But

Sorry it is not local that is problem, i forgot to pass length of arrays to multiplication.