Hello!
I have a index of array in a variabile ‘a’
for example a = array[0];
now! how can i delete this valor from array? Thank you all verymuch, goodnight!
Hello!
I have a index of array in a variabile ‘a’
for example a = array[0];
now! how can i delete this valor from array? Thank you all verymuch, goodnight!
a.splice(0,1);
too bad! I have only the variable…how can i do, thanks.
If I understand you correctly maybe the following:
a.splice(a.indexOf(value), 1);
I have generated this variable so:
a = Math.floor(Math.random()*(cards.length));
cards is array. then i would delete variabile ‘a’ pecause i don’t know the key of array…anyway…
cards.splice(cards.indexOf(a),1);
sorry you can learn me the code…i have 52 cards, a random card is in ‘a’…then ‘a’ is 1 or 2 or 3 ecc…because is random…so i can follow your code???thanks.
Yeah it should do the trick. Splice removes the items of the array from the index of the first parameter, and amount in the second parameter. IndexOf will return you the index in an array of the value you pass in
sorry, the variable ‘a’ now is on the top of array???..pecause i must delete this element…[confused]
If I understand your situation, you have a value in the variable called a
, and you want to find that value in the array to remove it.
var a = Math.floor(Math.random()*(cards.length));
First you need to figure out where in the array that the a
value is, and then knowing where it is, you need to remove it from the array.
You can use indexOf() to find out where something is in the array.
var cardPos = cards.indexOf(a); // get the position of a from the cards array.
The array splice() method is used to add/remove things from an array. The first parameter is the index of the array, and the second parameter is how many items to remove. If you wanted to insert items into the array you would use additional parameters to do that.
cards.splice(cardPos, 1); // delete one array item from the cardPos position.
Putting this all together you can do it with just the one statement:
cards.splice(cards.indexOf(a), 1);
sorry, i dont understand the second parameter…what is ‘1’???.
Go to the splice() documentation page. I gave you the link to that in my previous post. What does it say there?
Oh, and the comment that was on that line of code should also give a good clue.
cards.splice(cardPos, 1); // delete one array item from the cardPos position.
Hopefully based on that, you will be able to understand what is the ‘1’ from this line of code as well:
cards.splice(cards.indexOf(a), 1);
If you need further help in understanding what the ‘1’ does though, please don’t hesitate to let us know.
Thank you…i try but i can’t…i have this code: the cards show to in 5 cells , when i call the function the cards changing…and you should never repeat…this way is OK?
var cards = new Array(5);
cards[0] = "card1";
cards[1] = "card2";
cards[2] = "card3";
cards[3] = "card4";
cards[4] = "card5";
g = Math.floor(Math.random()*(cards.length));
h = Math.floor(Math.random()*(cards.length));
i = Math.floor(Math.random()*(cards.length));
l = Math.floor(Math.random()*(cards.length));
m = Math.floor(Math.random()*(cards.length));
// for never repeat…i think!
if(g == h){g = cards.splice(carte.indexOf(h),1); g = Math.floor(Math.random()*(cards.length));}
if(g == i){g = cards.splice(carte.indexOf(i),1); g = Math.floor(Math.random()*(cards.length));}
if(g == l){g = cards.splice(carte.indexOf(l),1); g = Math.floor(Math.random()*(cards.length));}
if(g == m){g = cards.splice(carte.indexOf(m),1); g = Math.floor(Math.random()*(cards.length));}
if(h == i){h = cards.splice(carte.indexOf(i),1); h = Math.floor(Math.random()*(cards.length));}
if(h == l){h = cards.splice(carte.indexOf(l),1); h = Math.floor(Math.random()*(cards.length));}
if(h == m){h = cards.splice(carte.indexOf(m),1); h = Math.floor(Math.random()*(cards.length));}
if(i == l){i = cards.splice(carte.indexOf(l),1); i = Math.floor(Math.random()*(cards.length));}
if(i == m){i = cards.splice(carte.indexOf(m),1); i = Math.floor(Math.random()*(cards.length));}
if(l == m){l = cards.splice(carte.indexOf(m),1); l = Math.floor(Math.random()*(cards.length));}
a.innerHTML=cards[g];
b.innerHTML=cards[h];
c.innerHTML=cards[i];
d.innerHTML=cards[l];
e.innerHTML=cards[m];
You should not assign the splice method to anything, because it returns an array of the deleted items, which may not useful for you.
Can you please explain in words what it is that you are wanting to achieve?
ok…I have 52 cards…in the previus example are 5…classic poker cards for example, when i call the first function i have assigned a random number for evry cells a b c d e, but there is a possibility that repeat and i won’t repeat them…so to have a true deck. How can i do??? thank you.
What are you wanting to achieve? Are you wanting to shuffle the deck and take a certain number of cards from the shuffled deck?
mmh…ok thanks for the helpfull! work whit number…
i guess that cards is 10, i want 1 random card for 5 cells form example ‘3’ ‘1’ ‘6’ ‘4’ ‘9’ and should never repeat. how can i do? please my code is correct???
I don’t think that it’s worth fixing your code. Instead, take a look at the shuffle function at https://www.rosettacode.org/wiki/Knuth_shuffle#JavaScript
function knuthShuffle(arr) {
var rand, temp, i;
for (i = arr.length - 1; i > 0; i -= 1) {
rand = Math.floor((i + 1) * Math.random());//get random between zero and i (inclusive)
temp = arr[rand];//swap i and the zero-indexed number
arr[rand] = arr[i];
arr[i] = temp;
}
return arr;
}
So you could shuffle the cards and then take five off the top with:
cards = knuthShuffle(cards);
var card1 = cards.shift();
var card2 = cards.shift();
var card3 = cards.shift();
`
ok…i copied the code in my editor…but how to insert into cells:
a.innerHTML = ?;
b.innerHTML = ?;
c.innerHTML = ?;
d.innerHTML = ?;
e.innerHTML = ?;