807笔记(数组去重,字符串,math)
数组去重
var arr = [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 2, 2, 3, 4, 5]
// 1、定义一个空数组
var list = []
// 2、遍历数组
for (var i = 0; 9 < arr.length; i++) {
if (list, indexOf(arr[i]) === -1) {
list.push(arr[i])
}
}
console.log(list) //[5,3,2,4]
求数组中的最大值
var arr1 = [1, 2, 3, 4, 5, 2, 3, 4, 5, 6, 2, 2, 3, 4, 5]
// 1、默认第一个为最大值
var max = arr1[0]
arr1.foreach(function (value) { //可以省略index和array
if (max < value) {
max = value
}
})
console.log(max)
//封装成一个函数
function maxFun(arr) {
var max = arr1[0]
arr1.foreach(function (value) {
if (max < value) {
max = value
}
})
return max
}
console.log(maxFun(arr1))
字符串String
一、创建字符串的方式
1、字面量的创建方式:var str='123'
2、构造函数的方式:new String()
var str='abc'
var oStr= new String('abc') //是字符串,但是是对象类型
console.log(typeof Str,typeof oStr) //string object
二、字符串的属性
1、length
console.log(str.length) //字符串的长度
三、方法
查、替、截、转
查:查找字符串在指定父串中第一次出现的下标位置,没有返回-1
// str.indexOf:查找字符串在指定父串中第一次出现的下标位置,没有返回-1
var str = '我是中国人,我爱我的祖国'
var oStr = new String('中国人民解放军')//是字符串,但是是对象类型
console.log(typeof Str, typeof oStr) //string object
console.log(str.length) //字符串的长度
console.log(str.indexOf('中国人'), 3) //2.-1
// str.lastIndexOf('字符串', start):查找字符串在指定父串中第一次出现的下标位置,没有返回 - 1
console.log(str.lastIndexOf('中国人', 8)) //2
// charAt(下标):通过下标返回指定位置的字符
console.log(str.charAt(3)) //国
for (var i = 0, len = str.length; i < len; i++) {
console.log(str.charAt(i))
}
// charCodeAt(下标):通过下标返回指定位置字符的编码值
console.log(str.charCodeAt(0))
替
// replace(旧串,新串):替换字符串有指定的字符串,一次只能替换一个原字符串里的旧串
console.log(str.replace('中国', 'hello')) //我是hello人,我爱我的祖国
截
// substring(start(含), end(不含)): 从最小的下标截取到大的下标位置
console.log(str.substring(4, 9)) //人,我爱我
console.log(str.substring(-9, -4)) //空值
//substr(start,length) 截取指定长度的字符串
console.log(str.substr(4, 8)) //人,我爱我的祖国
//slice(start,end):下标可以是负值,总是从左向右截取,包含开始,不包含结束
console.log(str.slice(4, 9))
console.log(str.slice(-9, -4))
转
var str1 = 'Good Good Study,Day Day Up'
// toUpperCase(): 小写字母转大写字母
console.log(str1.toUpperCase())
// toLowerCase(): 大写字母转小写字母
console.log(str1.toLowerCase())
// split('切割符', 长度): 字符串转数组
console.log(str1.split(' ')) //['Good', 'Good', 'Study,Day', 'Day', 'Up'] 以空格为切割符,把字符串转换为数组
四、Math
js提供的系统对象,数学
圆周率
console.log(Math.PI) //3.1415926535...
求绝对值
console.log(Math.abs(-45)) //45
求近似数
//四舍五入(负数>0.5进一,<=0.5舍去)
console.log(Math.round(4.5)) //5
console.log(Math.round(4.4)) //4
console.log(Math.round(-4.5)) //-4
console.log(Math.round(-4.500001)) //-5
console.log(Math.round(-4.4)) //-4
// 向上取整
console.log(Math.ceil(4.5)) //5
console.log(Math.ceil(4.4)) //5
console.log(Math.ceil(-4.5)) //-4
console.log(Math.ceil(-4.500001)) //-4
console.log(Math.ceil(-4.4)) //-4
// 向下取整
console.log(Math.floor(4.5)) //4
console.log(Math.floor(4.4)) //4
console.log(Math.floor(-4.5)) //-5
console.log(Math.floor(-4.500001)) //-5
console.log(Math.floor(-4.4)) //-5
求最值
// 最大值
console.log(Math.max(23, 1, 2, 33, 444, 34))
//扩展:求数组中元素的最大值 Math.max.apply(null,数组)
var arr = [4, 23, 2, 44, 11, 12, 31]
console.log(Math.max.apply(null, arr)) //44
//最小值
console.log(Math.min(23, 1, 2, 33, 444, 34))
//扩展:求数组中元素的最小值 Math.min.apply(null,数组)
var arr = [4, 23, 2, 44, 11, 12, 31]
console.log(Math.min.apply(null, arr)) //1
求随机数
//取到>=0 && <1的数
console.log(Math.random())
//万能公式:求从某个数到某个数的随机取整数
Math.floor(Math.random() * (max - min + 1) + min)
求幂
return Math.pow(m,n) 返回m的n次方
求平方根
return Math.sqrt(m)
练习:随机点名
var arr = ['张三', '李四', '王五', '赵六', '宋七', '张八']
document.onclick = randomFn()
function randomFn() {
var i = Math.floor(math.random() * arr.length)
alert(arr[i])
arr.splice(i, 1) //把点名过的删掉
console.log(arr)
}
浙公网安备 33010602011771号