📌  相关文章
📜  接受任何问题并从数组中返回随机选择的项目的函数 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:49.058000             🧑  作者: Mango

代码示例2
var a = ["Roger", "Russell", "Clyde", "Egbert", "Clare", "Bobbie", "Simon", "Elizabeth", "Ted", "Caroline"];

var chooseName = function () {
    var unique = true;
    num = Math.floor(Math.random() * a.length - 5);
    name = a.splice(num,1);
    a.push(name);
}


window.addEventListener("keypress", function (e) {
    var keycode = e.keyCode;
    if (keycode == 13) {
        chooseName();
    }
}, false);