0
How to log a random string from an array with a button?
Good evening everyone, I'm trying to create a function that can print a random quote from a variety of quotes in an array with the press of a button. For example: If I have an array like this: let quotes = ["To be or not to be", "Love yourself", "Be strong"] How do I create a function that can print one of those quotes randomly each time I press a button in Javascript? I'd prefer to not use any jQuery for this exercise. Thank you so much!
3 Réponses
+ 2
Well, you can use the random function of the Math class to select an integer between 0 and length of the quotes array to represent a random index to be selected.
Something like this:
function select() {
console.log(quotes[Math.floor(Math.random() * quotes.length)] )
}
+ 1
HeyItsJay
Please keep in mind, the Community helps those who help themselves.
Please show us some attempt you made to code this exercise.
I promise once you do you will have plenty of assistance.