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


Groups > comp.lang.javascript > #29219

Re: Local and global objects / variables in javascript

Newsgroups comp.lang.javascript
Date 2016-01-10 12:32 -0800
References <68d49145-aeb3-476b-9aae-2731fb18845b@googlegroups.com> <1bae5e3d-890b-4a2f-8cb0-e129a805e312@googlegroups.com> <3351aba0-ffb3-4b46-b61d-a78bb198228d@googlegroups.com> <c2240d3b-cc2b-4d00-9a77-d63561cbfe76@googlegroups.com>
Message-ID <11adc416-985d-4da5-8e7d-4cebb5f7940c@googlegroups.com> (permalink)
Subject Re: Local and global objects / variables in javascript
From jonas.thornvall@gmail.com

Show all headers | View raw


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>

Back to comp.lang.javascript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web