手记

javascript 数组 随手记

创建随机数组

function compare(){
return Math.random() - 0.5;
}
var num = [1,2,3,4,5,6];
console.log(num.sort(compare));

求中位数

let values = [2, 56, 3, 41, 0, 4, 100, 23];
    values.sort((a, b) => a - b);
    let lowMiddle = Math.floor((values.length - 1) / 2);
    let highMiddle = Math.ceil((values.length - 1) / 2);
    let median = (values[lowMiddle] + values[highMiddle]) / 2;
    console.log(median);
1人推荐
随时随地看视频
慕课网APP