1

Say I have arrays inside an array:

time = 0
arr = [[1,2,3], [4,5,6], [7,8,9]]

And I try to log the arr in each while loop

while (time<3){
  currentarr = JSON.parse(JSON.stringify(arr))

  for(i=0;i<currentarr.length;i++){
      for(j=0; j<currentarr[i].length; j++){

          if(currentarr[i][j]>2){
             arr[i][j]++
          }

      }
  }
  console.log(arr)
  time++
}

My problem is that each while loop gave me the same arr (the end result after three loops):

[[1, 2, 6],[7, 8, 9],[10, 11, 12]]

instead of for each time:

[[1, 2, 4],[5, 6, 7],[8, 9, 10]]  
[[1, 2, 5],[6, 7, 8],[9, 10, 11]] 
[[1, 2, 6],[7, 8, 9],[10, 11, 12]]

I just begin learning Javascript and I can't figure out which part went wrong... Thanks in advance!

1
  • 1
    Good question OP. Let me put you onto the let (item of arr) pattern, personally it saves my brain a lot of time compared to for (var i = 0; i < arr.length; i++) { arr[i] ... }, which is a lot less readable. Commented Jun 13, 2020 at 9:48

2 Answers 2

2

1 - It's important to use ;. Even if your code works in the environment that you are executing now, there are a lot of engines that won't be happy about that.

2 - Your while loop does not change any iteration inside it. If you pay attention, your time variable is not accessed any time. It's only used in the condition on the while loop and your time++ addition. It's only serving as a counter for your while loop, but it's not used anywhere inside your for loops as index for the arrays.

So you are basically executing the whole code inside your while loop 3 times, exactly the same, every time.

3 - Your first line currentarr = JSON.parse(JSON.stringify(arr)) creates a representation of your arr variable on that particular moment. Then you execute a comparison using this currentarr variable, on the line if(currentarr[i][j]>2){, but you add plus 1 on your original arr variable: arr[i][j]++.

I really doubt that's what you really intended.

Sign up to request clarification or add additional context in comments.

3 Comments

✊ Semicolon gang rise up. Two unnecessary sets of braces, but passed on the semicolons, tsk-tsk.
@Christian yeah, I thought to mention that, but let's give him/her some slack hehe. (but undoubtedly a clearer code: always semicolon, never unnecessary braces)
Thank you :) I'll be sure to use semicolons. For the third point, the code in this question has the same structure from my original code (which is too lengthy). The JSON line is intended there to create a deep copy. I just thought perhaps that would somehow cause my problem so I include it there. Thanks again!
1

Your code works as you expect. Your browser appears to be live-updating the logs.

From the MDN page on console.log():

Please be warned that if you log objects in the latest versions of Chrome and Firefox what you get logged on the console is a reference to the object, which is not necessarily the 'value' of the object at the moment in time you call console.log(), but it is the value of the object at the moment you open the console.

Also, from the same page:

Don't use console.log(obj), use console.log(JSON.parse(JSON.stringify(obj))).

This way you are sure you are seeing the value of obj at the moment you log it. Otherwise, many browsers provide a live view that constantly updates as values change. This may not be what you want.


This isn't related to your question, but I recommend you declare your variables. For example, instead of

for (i = 0; i < currentarr.length; i++)

do

for (let i = 0; i < currentarr.length; i++)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.