如何在JavaScript中同时使用random和split函数?
该函数应该输出带有逗号的随机字符,但没有输出如何在JavaScript中同时使用random和split函数?,javascript,Javascript,该函数应该输出带有逗号的随机字符,但没有输出 <p id="demo">Click the button to display a random number.</p> <button onclick="myFunction()">Submit</button> <script> function myFunction() { var random_list = randomizeList([1,2,3,4,49,'foo',38,
<p id="demo">Click the button to display a random number.</p>
<button onclick="myFunction()">Submit</button>
<script>
function myFunction()
{
var random_list = randomizeList([1,2,3,4,49,'foo',38,'bar',9,-1]);
var n=random_list.split(" ");
document.getElementById("demo").innerHTML=n;
}
</script>
单击按钮以显示随机数
提交
函数myFunction()
{
var random_list=randomizeList([1,2,3,4,49,'foo',38,'bar',9,-1]);
var n=随机列表。拆分(“”);
document.getElementById(“demo”).innerHTML=n;
}
如果计划使用拆分功能,则列表应为CSV字符串。拆分后,使用随机数获取数组的索引
HTML
<p id="demo">Click the button to display a random number.</p>
<button id="mybutton">Submit</button>
工作示例什么是randomizeList
?不只是拆分字符串吗?(假设从randomizeList()
返回的是一个数组)
var button = document.getElementById("mybutton");
button.onclick = function(){myFunction()}
function myFunction()
{
var random_list = "1,2,3,4,59,foo,38,bar,9,-1";
var n=random_list.split(",");
document.getElementById("demo").innerHTML=n[Math.floor((Math.random()*n.length))];
}