如何添加以下 JavaScript 代码(随机数生成器)?function randomNumber(max) { return Math.floor(Math.random() * max + 1);}const list = []while(list.length < 25) { let nbr = randomNumber(25) if(!list.find(el => el === nbr)) list.push(nbr)}console.log("list",list)进入这个文本区域并用 DOM 显示它们?<!DOCTYPE html><html><head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title></head><body> <textarea name="" id="" cols="30" rows="10"></textarea> <script src="app.js"></script></body></html>
1 回答
largeQ
TA贡献2039条经验 获得超7个赞
// Using an id to get the element
const textarea = document.getElementById('area');
function randomNumber(max) {
return Math.floor(Math.random() * max + 1);
}
const list = []
while(list.length < 25 ){
let nbr = randomNumber(25)
if(!list.find(el => el === nbr))
list.push(nbr)
}
// Set the value to the list
textarea.value = list;
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<textarea name="" id="area" cols="30" rows="10"></textarea>
<script src="app.js"></script>
</body>
</html>
添加回答
举报
0/150
提交
取消