2
I want to add 3 elements to each empty preference array using the splice method on the condition that when data1 has 3 elements it jumps to data2 and adds 3 elements and when data2 has 3 elements it jumps to data3 and adds until arr.length is equal to 0. That’s what I’ve wanted to say since yesterday, but I’m new to this world. I know it’s not hard but I’ve tried and I couldn’t. A thousand apologies to all who have answered my question.
What I want is to create a function so I can add the elements to the 3 empty arrays at the same time. Exactly I do not want to copy the elements of the arr I want to take from an array and add to the empty arrays. for example take the first 3 indices and add in array1, take 3 indices and add in array2, take the last 3 indices and add in array3 but all at the same time.
i want to create a simple card game but with images so I have this array of images and the Divs with different class representing each player as I could not pass the images directly to the Divs I decided to create 4 arrays for each player but then tb had problem pk n was able to implement the loop. In the game I have to shuffle the cards, deal the cards, predict the moves, add up the value of the cards can I do with javascript? In your opinion what the best way to do?
var arr = [];
arr[0]='<img class="images" src="1.jpg">';
arr[1]='<img class="images" src="2.jpg">';
arr[2]='<img class="images" src="3.jpg">';
arr[3]='<img class="images" src="4.jpg">';
arr[4]='<img class="images" src="5.jpg">';
arr[5]='<img class="images" src="6.jpg">';
arr[6]='<img class="images" src="7.jpg">';
arr[7]='<img class="images" src="8.jpg">';
arr[8]='<img class="images" src="9.jpg">';
arr[9]='<img class="images" src="10.jpg">';
arr[10]='<img class="images" src="11.jpg">';
arr[11]='<img class="images" src="12.jpg">';
The result would be this:
js
var data1 = ['elemento1','elemento2','elemento3'];
var data2 = ['elemento4','elemento5','elemento6'];
var data3 = ['elemento7','elemento8','elemento9'];
var data4 = ['elemento10','elemento11','elemento12'];
html
<div id="geral" >
<div id="posi1" class="div_um"></div>
<div id="posi2" class="div_dois"></div>
<div id="posi3" class="div_tres"></div>
<div id="posi4" class="div_quatro"></div>
</div>
Can you explain better what you want to do? It’s unclear. How so "I don’t want to make a copy"?
– Sergio
If you also explain the context we can help in the best way to do what you need.
– Sergio
what I want is to create a function so I can add the elements to the 3 empty arrays at the same time. Exactly I do not want to copy the elements of the arr I want to take from an array and add to the empty arrays. for example take the first 3 indices and add in array1, take 3 indices and add in array2, take the last 3 indices and add in array3 but all at the same time. theoretically I know how to do it but in practice I could not because I am asking for help from you.
– magestik
magestik: I’d still like to leave one more answer but I’d like you to explain where these images come from (this is if the chicks are by hand or as the
arr
is filled) and also how you will usedata1
,data2
anddata3
. Knowing this I can leave a code/logic suggestion to use.– Sergio
Sérgio I’m learning Javascript and I don’t know if I’ll explain it correctly pk there are terms I don’t know. But this is how these images come from .../images that will then be inserted into a div through data1, so data1 corresponds to a div, data2 corresponds to a div and data1 corresponds to a div. But I didn’t explain myself well so it’s hard to understand.
– magestik
magestik: OK, and these Divs already exist in HTML? Can you put an example of the code? so you could loop over those images that you would distribute directly into each div. We just need to figure out what the code of these Divs is. If you have a specific class or a common relative.
– Sergio
I’ll explain what my purpose is with all this, i want to create a simple card game but with images so I have this array of images and the Divs with different class representing each player as I could not pass the images directly to the Divs I decided to create 4 arrays for each player but then tb had problem pk n was able to implement the loop. In the game I have to shuffle the cards, deal the cards, predict the moves, add up the value of the cards can I do with javascript? In your opinion what is the best way to do? I’m still getting started, Thank you.
– magestik
Good! so I have the context I asked for. And do you already have any HTML I can use in an example in my answer? or are you starting from scratch? The other steps you have difficulty asking that we help.
– Sergio
Look you can use your html msm, I want in the examples comment the pk code interests me to understand the code
– magestik