js实现根据要求生成指定数量的随机中文姓名代码
代码语言:html
所属分类:其他
代码描述:js实现根据要求生成指定数量的随机中文姓名代码,可指定性别、姓氏和名字长度以及数量生成随机的中文姓名。
代码标签: js 根据 要求 生成 指定 数量 随机 中文 姓名 代码
下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开
<!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>生成随机中文姓名</title> <script> const surnames = [ "王", "李", "张", "刘", "陈", "杨", "赵", "黄", "周", "吴", "徐", "孙", "胡", "朱", "高", "林", "何", "郭", "马", "罗" ]; const givenNamesMale = [ "伟", "磊", "洋", "勇", "军", "杰", "涛", "明", "刚", "强" ]; const givenNamesFemale = [ "芳", "娜", "敏", "静", "丽", "娟", "霞", "秀英", "华", "平" ]; function getRandomInt(max) { return Math.floor(Math.random() * max); } function generateRandomChineseName(surname, length, gender) { let givenNames = gender === "male" ? givenNamesMale : givenNamesFemale; let name = surname; for (let i = 0; i < length; i++) { name += givenNames[getRandomInt(givenNames.length)]; } return name; } function generateNames() { let surname = document.getElementById("surname").value; let length = parseInt(document.getElementById("length").value); let gender = document.querySelector('input[name="gender"]:checked').value; let count = parseInt(document.getElementById("count").value); let result = document.getElementById("result"); result.innerHTML = ""; for (let i = 0; i < count; i++) { let name = generateRandomChineseName(surname, length, gender); let li = document.createElement("li"); li.textContent = name; result.appendChild(li); } } </script> </head> <body> <h1>生成随机中文姓名</h1> <label for="sur.........完整代码请登录后点击上方下载按钮下载查看
网友评论0