How to randomly select a name in an array list
P粉418351692
P粉418351692 2023-07-19 09:17:02
0
1
574

I encountered a problem about an array list. I wanted to return a random name in the array list, but I encountered a problem when implementing it.

``function randomName(names) {
names = ["john", "Ben", "Jenny", "Michael", "Chloe"];
Math.random(names.length);
console.log(names);

    return names + "is going to buy lunch today!";

}

randomName();

I want the output to be:

Michael (or any name in the array) is going to buy lunch today!

Can you guide me on how to achieve this?


P粉418351692
P粉418351692

reply all(1)
P粉455093123
const index = Math.floor(Math.random() * names.length);

names[index]
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template