Q:

javascript slice within loop for array

I want to take an array output like the following:
 [a,
  b,
  c,
  d,
  e,
  f]

and slice it into sections, then output each new array on a newline like this: 
 [a,b
  c,d
  e,f]

Here's what i have so far: 
var data = "";
for (i = 0; i < 4; i+=2) {
data += output.slice(i,2);

}
return data;

I've been working at this for some time now trying different things to make this work but no luck. I'm new to JS, so i need some help.

A:

Since you have six elements in your array, you need to iterate to 6 not 4. The best way to do that is to use the length of your array, so if the length changes you don't need to change the loop. When taking a slice you also want slice(i, i+2)
If you just want to make a string, you can add a \n character in each iteration:

let output = ['a', 'b', 'c', 'd', 'e', 'f']

var data = "";
for (i = 0; i < output.length; i+=2) { 
    data += output.slice(i,i+2).join(" ") + "\n";
}
console.log(data);

