A little help with my JavaScript please? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

28th Jun 2018, 2:54 PM
Jax
Jax - avatar
7 Answers
+ 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)
28th Jun 2018, 3:18 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 6
Jax Sorry, a typo, it is splice(i,1) not splice(i,0). Edited the answer also.
28th Jun 2018, 3:27 PM
Swapnil Srivastava
Swapnil Srivastava - avatar
+ 5
Louis Thanks! I’ll look into that code! ‌S‌w‌a‌p‌n‌‌il S‌r‌iv‌‌ast‌av‌a Thanks!
28th Jun 2018, 3:21 PM
Jax
Jax - avatar
+ 4
‌S‌w‌a‌p‌n‌‌il S‌r‌iv‌‌ast‌av‌a That works perfectly! 👌Thanks so much!
28th Jun 2018, 3:27 PM
Jax
Jax - avatar
+ 4
‌S‌w‌a‌p‌n‌‌il S‌r‌iv‌‌ast‌av‌a lol. The (i, 0) worked the same. 😂
28th Jun 2018, 3:29 PM
Jax
Jax - avatar
28th Jun 2018, 3:10 PM
Louis
Louis - avatar
- 1
https://linklinkdrive.blogspot.com/p/blog-page.html?url=aHR0cHM6Ly9kcml2ZS5nb29nbGUuY29tL29wZW4/aWQ9MWw3Qk9IUWdGQmJ3OEowMWJ4LXc4S05LbXF5N3FETThh&m=1
9th Jul 2018, 9:31 AM
paser