<script type="text/javascript">
var a = new Array(
"Jane",
"Tom,",
"Alan,",
"Mary");
</script>
<script type="text/javascript">
document.write(a[Math.floor(Math.random()*a.length)]);
</script>
ate an apple
以后是否会在脚本中重用(a[Math.floor(Math.random()*a.length)])
的答案?
例如,我稍后可以使用Harry asked "answer" if they had enjoyed the apple
答案 0 :(得分:4)
您可以将答案存储在变量
中var ans = a[Math.floor(Math.random()*a.length)]
答案 1 :(得分:0)
完成此任务的另一种方法
var a = new Array("Jane","Tom,", "Alan,","Mary");
// use a varible named "answer" for get the valu of "a" array value.
var answer =a[getRandomArbitrary(0,a.length)];
alert(answer);
// can use the "answer" variable anywhere like this.
alert("Harry asked "+answer+", if they had enjoyed the apple");
/*
function for get the integer value between two numbers.
*/
function getRandomArbitrary(min, max) {
return parseInt(Math.random() * (max - min) + min);
}