其他分享
首页 > 其他分享> > js 判断 数字 数组 单调递增

js 判断 数字 数组 单调递增

作者:互联网

直接看代码

  const isSorted = (nums: number[]) => {
    return nums.every((x, i) => i === 0 || x >= nums[i - 1])
  }

如果想判断 是否是单调递减 传数组之前 数组.reverse() 反向一下 就行

测试

    const nums1 = [1, 2, 3, 4, 5, 6]
    const nums2 = [1, 2, 3, 5, 5, 1]
    let b1 = isSorted(nums1);
    console.log("b1", b1)
    let b2 = isSorted(nums2);
    console.log("b2", b2)

结果没问题

标签:const,数组,nums,递增,js,b1,b2,单调,isSorted
来源: https://www.cnblogs.com/ifnk/p/16316172.html