I don't get why this causes an infinite loop

I don't get why this causes an infinite loop
0.0 0

#1

can someone explain why it says this causes and infinite loop?

function chunkArrayInGroups(arr, size) {
  // Break it up.
  var arr2 = [];
  
for (var x = 0; x < arr.length; x + size) {
 
 arr2.push(arr.slice(x, (x + size)));
  
}

  return arr2;
}

chunkArrayInGroups(["a", "b", "c", "d"], 2);

Thank you


#2

In your for loop x doesn’t change.

x = 2 every time it checks for x <arr.length.

if you want to change x each loop use +=.


#3

the += means with a changed value?

Thank you!


#4

Yup shorthand for x = x + whatever;
Example:

let x  = 1;
console.log(x) // 1
console.log(x+1)// 2
x += 1;

console.log(x) // 2

#5

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.