基本概念之六

数组操作与随机数生成

方法 - 把数组元素连接成一个字符串
.join() - .join(value) sample .join("*")

 

var landmarks = [];
landmarks.push("My House");
landmarks.push("Front path");
landmarks.push("Flikering streetlamp");
landmarks.push("Leaky fire hydrant");
landmarks.push("Fire station");
landmarks.push("Cat Rescue Center");
landmarks.push("My Old School");
landmarks.push("My friend's house");

产生随机数的方法 Math.random();得到0-1之间的一个随机小数
要产生0-10之间的随机小数,只要Math.random()*10
要取得一个长度为15的数组的随机索引,只要Math.floor(Math.random()*15)
Match.floor()只是截取整数部分,不是四舍五入

var randomWords = ["Explosion","Cave","Princess","Pen"];
randomWords[Math.floor(Math.random()*4)];

决策者程序之一

var phrases = [
"That sounds good.",
"Yes, you should definitely do that.",
"I'm not sure that's a great idea.",
"Maybe not today?",
"Computer says no."
];
phrases[Math.floor(Math.random()*5)];

转载于:https://www.cnblogs.com/balian/p/5968383.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值