+ 7
A little help with my JavaScript please?
https://code.sololearn.com/WIkQqpUVeS3I/?ref=app My code has an array with a length of 10, and the code picks a random number based on the array’s length. I then delete an item from the Array, to prevent duplicates, however, the Array length does not change, and just gives me a whole bunch of undefines. I need the code to only pick from cards that haven’t been chosen and stop when all have been picked, but I can’t figure out how to do this.
7 ответов
+ 9
You can use Array.prototype.splice instead.
monsters.splice(i,1) instead of delete monsters[i]
EDIT : splice(i,1) not splice(i,0)
+ 6
Jax Sorry, a typo, it is splice(i,1) not splice(i,0). Edited the answer also.
+ 5
Louis
Thanks! I’ll look into that code!
Swapnil Srivastava
Thanks!
+ 4
Swapnil Srivastava
That works perfectly! 👌Thanks so much!
+ 4
Swapnil Srivastava
lol. The (i, 0) worked the same. 😂
+ 3
See similar example here
https://code.sololearn.com/WF44SaccHLvv/?ref=app
- 1
https://linklinkdrive.blogspot.com/p/blog-page.html?url=aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MWw3Qk9IUWdGQmJ3OEowMWJ4LXc4S05LbXF5N3FETThh&m=1