Hello CodeNewbie! I'm teaching myself JavaScript right now and am having a hard time with selecting a random item from a JavaScript array. Can anyone help illuminate the issue for me?
Cheers!
Hello CodeNewbie! I'm teaching myself JavaScript right now and am having a hard time with selecting a random item from a JavaScript array. Can anyone help illuminate the issue for me?
Cheers!
For further actions, you may consider blocking this person and/or reporting abuse
jonathandaveiam -
Prachi desai -
Dexodata -
AZ 900 Dumps -
Once suspended, harrycoder will not be able to comment or publish posts until their suspension is removed.
Once unsuspended, harrycoder will be able to comment and publish posts again.
Once unpublished, all posts by harrycoder will become hidden and only accessible to themselves.
If harrycoder is not suspended, they can still re-publish their posts from their dashboard.
Once unpublished, this post will become invisible to the public and only accessible to Harry.
They can still re-publish the post if they are not suspended.
Thanks for keeping CodeNewbie Community 🌱 safe. Here is what you can do to flag harrycoder:
Unflagging harrycoder will restore default visibility to their posts.
Top comments (1)
Hello Harry.
So array indices go from 0 to array.length - 1. When selecting a random item, we want to get a random integer (whole number) within that range.
Knowing this, we can define a function
getRandomElement
that takes in an array and returns a random element from the given array. I defined it as a function assuming you would use this more than once. It's good practice to do this to avoid repeating code (DRY: Don't Repeat Yourself).Resources:
Math.floor
Math.random