Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #29202 > unrolled thread

Local and global objects / variables in javascript

Started byjonas.thornvall@gmail.com
First post2016-01-10 00:53 -0800
Last post2016-01-10 14:57 -0800
Articles 6 — 2 participants

Back to article view | Back to comp.lang.javascript


Contents

  Local and global objects / variables in javascript jonas.thornvall@gmail.com - 2016-01-10 00:53 -0800
    Re: Local and global objects / variables in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-10 08:30 -0800
      Re: Local and global objects / variables in javascript jonas.thornvall@gmail.com - 2016-01-10 09:55 -0800
        Re: Local and global objects / variables in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-10 10:54 -0800
          Re: Local and global objects / variables in javascript jonas.thornvall@gmail.com - 2016-01-10 12:32 -0800
            Re: Local and global objects / variables in javascript "Michael Haufe (TNO)" <tno@thenewobjective.com> - 2016-01-10 14:57 -0800

#29202 — Local and global objects / variables in javascript

Fromjonas.thornvall@gmail.com
Date2016-01-10 00:53 -0800
SubjectLocal and global objects / variables in javascript
Message-ID<68d49145-aeb3-476b-9aae-2731fb18845b@googlegroups.com>
I've been fiddling with javascript for quite a while but i still do not grasp the correct way to make an array local in javascript slice() can't possibly be the correct way?

function naiveAdd(base, arrOne, arrTwo)
{
   addOne=arrOne.slice();  //Is this the correct way to make addOne local?
   addTwo=arrTwo.slice();  //If you put addOne,addTwo in head,they still affect 
   abigArr = addOne.length;//the global arrOne, arrTwo in the function call
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      
      addOne[i] = addOne[i] + addTwo[i] + remainder;
      if (addOne[i] >= base)
      {  
         
         addOne[i] = addOne[i] - base;
         remainder = 1;
      
      }
      else
      {
         remainder = 0;
      }
   }
   // If strings of equal length but there is a remainder;
   while (remainder == 1 && addOne[i]==(base-1))
   {
      addOne[i] = 0;
      i++;
   }
   if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;}
   return addOne;
}

[toc] | [next] | [standalone]


#29212

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-01-10 08:30 -0800
Message-ID<1bae5e3d-890b-4a2f-8cb0-e129a805e312@googlegroups.com>
In reply to#29202
On Sunday, January 10, 2016 at 2:54:05 AM UTC-6, jonas.t...@gmail.com wrote:
> I've been fiddling with javascript for quite a while but i still do not grasp the correct way to make an array local in javascript slice() can't possibly be the correct way?

use var, let, or const to declare variables, depending on the version of ECMAScript you're using

[toc] | [prev] | [next] | [standalone]


#29214

Fromjonas.thornvall@gmail.com
Date2016-01-10 09:55 -0800
Message-ID<3351aba0-ffb3-4b46-b61d-a78bb198228d@googlegroups.com>
In reply to#29212
Den söndag 10 januari 2016 kl. 17:30:56 UTC+1 skrev Michael Haufe (TNO):
> On Sunday, January 10, 2016 at 2:54:05 AM UTC-6, jonas.t...@gmail.com wrote:
> > I've been fiddling with javascript for quite a while but i still do not grasp the correct way to make an array local in javascript slice() can't possibly be the correct way?
> 
> use var, let, or const to declare variables, depending on the version of ECMAScript you're using

But is the slice the correct way to do local copies of the array?

[toc] | [prev] | [next] | [standalone]


#29215

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-01-10 10:54 -0800
Message-ID<c2240d3b-cc2b-4d00-9a77-d63561cbfe76@googlegroups.com>
In reply to#29214
On Sunday, January 10, 2016 at 11:55:15 AM UTC-6, jonas.t...@gmail.com wrote:

> But is the slice the correct way to do local copies of the array?

 Array.prototype.slice() returns a shallow copy of an array.

[toc] | [prev] | [next] | [standalone]


#29219

Fromjonas.thornvall@gmail.com
Date2016-01-10 12:32 -0800
Message-ID<11adc416-985d-4da5-8e7d-4cebb5f7940c@googlegroups.com>
In reply to#29215
Den söndag 10 januari 2016 kl. 19:54:23 UTC+1 skrev Michael Haufe (TNO):
> On Sunday, January 10, 2016 at 11:55:15 AM UTC-6, jonas.t...@gmail.com wrote:
> 
> > But is the slice the correct way to do local copies of the array?
> 
>  Array.prototype.slice() returns a shallow copy of an array.

Well i have to read up on javascript objects and prototype some day.
Any suggestion upon improvement of speed or code quality appreciated. 

<script language="Javascript">

/*ADD TWO VARIABLES USING CHOSEN BASE*/
function naiveAdd(base, arrOne, addTwo)
{
   addOne=arrOne.slice();
   //addTwo=arrTwo.slice(); 
   abigArr = addOne.length;
   asmallArr = addTwo.length;
   addResult = [];
   remainder = 0;
   for (i = 0; i < asmallArr; i ++ )
   {
      
      addOne[i] = addOne[i] + addTwo[i] + remainder;
      if (addOne[i] >= base)
      {  
         
         addOne[i] = addOne[i] - base;
         remainder = 1;
      
      }
      else
      {
         remainder = 0;
      }
   }
   // If strings of equal length but there is a remainder;
   while (remainder == 1 && addOne[i]==(base-1))
   {
      addOne[i] = 0;
      i++;
   }
   if (remainder == 1) {if (isNaN(addOne[i])) addOne[i] = 0; addOne[i] = addOne[i]+1;}
   return addOne;
}

/* COMPARE SIZE OF TWO ARRAYS, IF A < B RETURN TRUE, IF B >= A RETURN FALSE */
function lessThan(A, B)
{
   var AA = A.length;
   var BB = B.length;
   if(AA > BB) return false;
   if(AA < BB) return true;
   // AA = BB, compare indexes from biggest to smallest.
   for(i = AA - 1; i >= 0; i -- )
   {
      if(A[i] < B[i]) return true;
      if(A[i] > B[i]) return false;
   }
   return false;
}

/* ORDER THE ARRAYS SO THE BIGGER IS PASSED AS FIRST ARGUMENT*/
function orderArrayAdd(Abase, A, B)
{
   return lessThan(A, B) ? counterArr = naiveAdd(Abase, B, A) : counterArr = naiveAdd(Abase, A, B);
}

/* GET VALUES FROM INPUT*/
function fetchValues(){
base=document.eval.FBASE.value;
myeval = document.eval.expression.value;
lucasStart = document.eval.start.value;
lStart=parseInt(lucasStart)
lucasScope = document.eval.scope.value;
lScope=parseInt(lucasScope)
}

/* PARSE VALUES FROM INPUT */
function parseToInt(){
if (result = myeval.indexOf("+") != - 1)
   {
      opArr = myeval.split("+");
      operation = "+";
   }
   arrOne = opArr[0].split("").map(Number).reverse();
   arrTwo = opArr[1].split("").map(Number).reverse();
}


function main(){
document.eval.result.value="";
document.eval.timing.value="";

base=2;
evalStr="";
var out=[];counter=1;
fetchValues();
parseToInt();
flip=0;
/* TIMER START */
var start = new Date().getTime();
lEnd=lStart+lScope;

while(counter<=lEnd){
out=orderArrayAdd(base,arrOne,arrTwo);
arrOne=arrTwo.slice();
arrTwo=out.slice();
if (counter>=lStart && counter<lEnd) {fib=out.slice();evalStr+=counter+"th "+fib.reverse().join('')+"\n";}
counter++;
}
/* TIMER END */
var end = new Date().getTime();
var time = end - start;
document.eval.result.value+=evalStr;
document.eval.timing.value+=time;
} </script>

<html><body onLoad="main()";>
<H1>FIBONACCI AND OTHER SERIES</H1>

<form name="eval" onsubmit="main(); return false;">
Calculation Time<input type="text" name="timing"  value="" size="4"> ms <P>
<input type="submit" value="Generate"><br>

CHOSE BASE <input type="text" name="FBASE"  value="2" size="2"><P>
Start print x'th number in Serie<input type="text" name="start" value="1" size="9"> How many follwing in Serie -><input type="text" name="scope" value="777" size="9"><br>
Generate Serie expansons using startvalues<input type="text" name="expression" value="1+1" size="10"><br>
Result <textarea name="result" cols="100" rows="30"></textarea><br>


</form>
</body></html>

[toc] | [prev] | [next] | [standalone]


#29221

From"Michael Haufe (TNO)" <tno@thenewobjective.com>
Date2016-01-10 14:57 -0800
Message-ID<2697e049-9211-465b-9acd-0c12e69e0511@googlegroups.com>
In reply to#29219
On Sunday, January 10, 2016 at 2:32:39 PM UTC-6, jonas.t...@gmail.com wrote:
 
> Well i have to read up on javascript objects and prototype some day.
> Any suggestion upon improvement of speed or code quality appreciated. 

My suggestion is to "read up on javascript" before continuing. It will probably answer questions you didn't think to ask at this point. Even a so-so book would probably help a bit. For example:

<http://eloquentjavascript.net>

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.javascript


csiph-web