简体   繁体   中英

What is the correct way of reusing a function in for loop without resetting variables in javascript?

I came across this problem when I was trying to optimize my code. I am trying to use a function in my code (seems like I have to repeat code 2 times to make it work, because I am not getting the result I want):

 let tmpCont = undefined; let tmpPrice = undefined; let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { evaluate = analize(cont, tmp); console.log(evaluate + "test"); evaluate2 = analize(cont2, tmp2); console.log(evaluate2); } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

Result I am getting (what I dont want):

10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]
10,truetest
[ 30, false ]

My question is: Am I doing something wrong? I know why I am getting the result I dont want and I think something is missing BUT do i have to repeat code to get the result below?

Result I want:

10,truetest
[ 40, false ]
20,truetest
[ 50, false ]
30,truetest
[ 60, false ]
40,truetest
[ 70, false ]
50,truetest
[ 80, false ]
60,truetest
[ 90, false ]
70,truetest
[ 100, false ]
80,truetest
[ 110, false ]
90,truetest
[ 120, false ]
100,truetest
[ 130, false ]

In javascript all variables passed into a function are passed by value not by reference. Changing the value in a function will not change the value of a variable passed. You have 2 options:

  1. Pass an object containing your counts.
  2. Assign the returned values.

so to do the first:

 let evaluate = undefined; let evaluate2 = undefined; const values={cont:0, price:true}; const values2={cont:30, price:false}; for (let i = 0; i < 10; i++) { evaluate = analize(values); console.log(evaluate + " test"); evaluate2 = analize(values2); console.log(evaluate2); } function analize(tmp) { for (let i = 0; i < 10; i++){ tmp.cont++; } return [tmp.cont, tmp.price]; } 

or:

 let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { [cont,tmp] = analize(cont, tmp); console.log([cont,tmp] + " test"); [cont2, tmp2]= analize(cont2, tmp2); console.log([cont2, tmp2]); } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

You could just increment the values at the end of each for loop

 let tmpCont = undefined; let tmpPrice = undefined; let evaluate = undefined; let evaluate2 = undefined; let cont = 0; let tmp = true; let cont2 = 30; let tmp2 = false; for (let i = 0; i < 10; i++) { evaluate = analize(cont, tmp); console.log(evaluate + "test"); evaluate2 = analize(cont2, tmp2); console.log(evaluate2); cont += 10 // this cont2 += 10 // and this } function analize(tmpCont, tmpPrice) { for (let i = 0; i < 10; i++) { tmpCont++; } return [tmpCont, tmpPrice]; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM