[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.javascript

Local and global objects / variables in javascript

JT

1/10/2016 8:54:00 AM

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;
}
5 Answers

Michael Haufe (\"TNO\")

1/10/2016 4:31:00 PM

0

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

JT

1/10/2016 5:55:00 PM

0

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?

Michael Haufe (\"TNO\")

1/10/2016 6:54:00 PM

0

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.

JT

1/10/2016 8:32:00 PM

0

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>

Michael Haufe (\"TNO\")

1/10/2016 10:57:00 PM

0

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://eloquentjavascri...