1:a[]的length是由o[]中最大数决定的,在循环o没出现重复值之前都是在为a赋值,附的值并不是值的本身而是数出现的次数,出现重复值为该次数+1。
2:这个程序本身还存在一定的问题,以0打头的数是判断不到的,这样a[]中真正接收到的数其实只有15个(包括重复项)--------a[]中所有数相加为15
3:仔细观察(o[]中的值+1)== a[]中所在位置
目的:
就是找出o[]中大于等于10的重复项次数 重复项值为a[]中对应的索引值+1
给你把a[]贴出来可以观察到
[undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 1, 2, 1, undefined, undefined, undefined, undefined, undefined, undefined, 1, 1, 1, undefined, undefined, 1, undefined, undefined, undefined, undefined, undefined, 1, 2, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 2, undefined, undefined, undefined, 1, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, 1]
传统的阶层型遍历你这种点对点效率应该要高点
javascript是区分大小写的,其中的数组对象是Array,不是array。var array1=new Array("1","2","3","4","5","6","7","8","9")
问题:已知数组 arr = [1,9,3,7,5] ,使用程序计算出该数组的最大值,封装成函数
解答:
function arrMax(arr){var num=arr[0]
for(var i=1i<arr.lengthi++){
if(arr[i]>=num) num=arr[i]
}
return num
}
用法示例:
"use strict"var arr=[1,9,3,7,5]
function arrMax(arr){
var num=arr[0]
for(var i=1i<arr.lengthi++){
if(arr[i]>=num) num=arr[i]
}
return num
}
console.log(arrMax(arr))
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)