我遇到了一個關於數組列表的問題,我想在數組列表中返回一個隨機的名字,但是我在實現時遇到了問題。
``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();
我希望輸出結果是:
Michael (or any name in the array) is going to buy lunch today!
能指導我如何實現這個嗎?
P粉4550931232023-07-20 00:21:31
const index = Math.floor(Math.random() * names.length); names[index]