文本区域中的随机数生成器

如何添加以下 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>


aluckdog
浏览 186回答 1
1回答

largeQ

// Using an id to get the elementconst textarea = document.getElementById('area');function randomNumber(max) {&nbsp; &nbsp; return Math.floor(Math.random() * max + 1);}const list = []while(list.length < 25 ){&nbsp; &nbsp; let nbr = randomNumber(25)&nbsp; &nbsp; if(!list.find(el => el === nbr))&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; list.push(nbr)}// Set the value to the listtextarea.value = list;<!DOCTYPE html><html><head>&nbsp; &nbsp; <meta charset="UTF-8">&nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1.0">&nbsp; &nbsp; <title>Document</title></head><body>&nbsp; &nbsp; <textarea name="" id="area" cols="30" rows="10"></textarea>&nbsp; &nbsp; <script src="app.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript