How to deduce in a cycle on JS album list?

Experts in JS. Tell me it seems to be a trivial task. Can't put the not in php not in js. There is an array of 12 elements. In a loop looping through the array elements to specify the condition that every 2 units output the auxiliary block 12 % 2 === 0 How many cycles to output auxiliary units 7 and 12 records?

div
0
1
/div

div
2
3
/div

div
4
5
/div

div
6
7
/div

div
8
9
/div

div
10
11
/div

div
11
/div

5c9a72d5eaf66977588738.jpeg
March 19th 20 at 08:44
2 answers
March 19th 20 at 08:46
Solution
const data = [...Array(12)].map((n, i) => i + 1);

const chunkSize = 2;

const chunks = data.reduce((acc, n, i) => {
 if (!(i % chunkSize)) {
acc.push([]);
}
 acc[acc.length - 1].push(n);
 return acc;
}, []);

const html = chunks.map(n => `<div>${n.map(m => `<div>${m}</div>`).join(")}</div>`).join(");
Thank you very much! - buford commented on March 19th 20 at 08:49
March 19th 20 at 08:48
Solution
something I did not understand that where it is necessary to withdraw

doublediv = []
arr = [0,1,2,3,4,5,6,7,8,9,10,11,12]

for (let i = 0; i < arr.length; i++) {
 if(doublediv.length < 2) {
doublediv.push(arr[i])
 } else {
console.log(doublediv)
 console.log('extra div')
 doublediv = [arr[i]]
}
}


so?

5c9a79dda48f6848917124.png
Yes, thank you very much! Did not insert via innerHTML

<div class="box_album" id="box_album">

</div>


<div class="col_album row"> - this auxiliary div) 
 <div class="album-row">
 <div class="cover_album">
 <img src="images/cover_drippin_in_finesse.jpg">
</div>
 <div class="info_album">
 the <h3>Drippin in Finesse</h3>
 <h2>Lil Fil</h2>

<hr>

 <p>the Album contains 6 tracks</p>
</div>
</div>

 <div class="album-row">
 <div class="cover_album">
 <img src="images/cover_hard_way.jpg">
</div>
 <div class="info_album">
 the <h3>Hard Way</h3>
 <h2>Lil Fil</h2>

<hr>

 <p>the Album contains 6 tracks</p>
</div>
</div>
 </div>
- buford commented on March 19th 20 at 08:51
@buford, generally there is a suspicion that you want to display two albums in a row just?
maybe it's easier to do via css, if so

https://codepen.io/anon/pen/aMrNvB - kendrick.Bashirian50 commented on March 19th 20 at 08:54
@buford, it is possible to add the divas so

html = `<div class="row col_album">.....` // let here is the whole khtml diva
newDiv = document.createElement('div') // next element
newDiv.innerHTML = html // it throw contents
document.querySelector('#my-albums-wrapper').appendChild(newDiv) // and add


out more clearly than if everything is done type wrapperHTML.innerHTML = wrapperHTML.innerHTML + `.....` - kendrick.Bashirian50 commented on March 19th 20 at 08:57
@kendrick.Bashirian50, thank you Yes, that's right, two albums in a row just from me then json will be converted to css and js not mastered(

for(var a = 0; album.id_album.length > a; a++)
{
if(a % 2 === 0)
{
 box_album.innerHTML += '<div class="col_album row">';

 box_album.innerHTML += '</div>';
}*/

/*else
{
 box_album.innerHTML += 
 '<div class="album-row">' +
 '<div class="cover_album">' +
 '<img src="' + album.id_album[a].cover + '">' +
 '</div>' +
 '<div class="info_album">' +
 '<h3>' + album.id_album[a].album + '</h3>' +
 '<h2>' + album.id_album[a].artist + '</h2>' +
 '<hr>' +
 '<p>the Album contains 6 tracks</p>' +
 '</div>' +
'</div>';
 }


var album =
{
 'id_album' :
[
{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'Drippin in Finesse',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'HARD WAY',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'PIXEL',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'ATMOSPHERIC',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'AK-47',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'KATANA BLADE',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'CHAINS',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'OUTER SPACE',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'ILLUSIONS',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'FLEXIN',
 artist : 'Lil Fil',
},

{
 cover : 'images/cover_drippin_in_finesse.jpg',
 album : 'FLEXIN',
 artist : 'Lil Fil',
}
]
 }
- buford commented on March 19th 20 at 09:00
@buford, yo
https://codepen.io/anon/pen/xBNOyB

or just more careful
https://codepen.io/anon/pen/PLvGjJ - kendrick.Bashirian50 commented on March 19th 20 at 09:03

Find more questions by tags JavaScript