Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #27622 > unrolled thread
| Started by | jonas.thornvall@gmail.com |
|---|---|
| First post | 2015-05-14 06:55 -0700 |
| Last post | 2015-05-14 10:47 -0700 |
| Articles | 5 — 2 participants |
Back to article view | Back to comp.lang.javascript
Swap arrays ones again. jonas.thornvall@gmail.com - 2015-05-14 06:55 -0700
Re: Swap arrays ones again. jonas.thornvall@gmail.com - 2015-05-14 07:12 -0700
Re: Swap arrays ones again. Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-05-14 16:12 +0100
Re: Swap arrays ones again. jonas.thornvall@gmail.com - 2015-05-14 10:17 -0700
Re: Swap arrays ones again. jonas.thornvall@gmail.com - 2015-05-14 10:47 -0700
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-05-14 06:55 -0700 |
| Subject | Swap arrays ones again. |
| Message-ID | <974af16e-3a67-4d6c-944a-af27660f05a8@googlegroups.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?
[toc] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-05-14 07:12 -0700 |
| Message-ID | <c06cbc73-fe97-4d99-bf44-8ebe3a2532be@googlegroups.com> |
| In reply to | #27622 |
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.
[toc] | [prev] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2015-05-14 16:12 +0100 |
| Message-ID | <87zj572nxe.fsf@bsb.me.uk> |
| In reply to | #27622 |
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.
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-05-14 10:17 -0700 |
| Message-ID | <34ac83a6-3175-48eb-982c-fcfbe2919890@googlegroups.com> |
| In reply to | #27625 |
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
[toc] | [prev] | [next] | [standalone]
| From | jonas.thornvall@gmail.com |
|---|---|
| Date | 2015-05-14 10:47 -0700 |
| Message-ID | <acd08d23-6995-481f-9d69-531d096b53a5@googlegroups.com> |
| In reply to | #27628 |
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.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.javascript
csiph-web