严格增加或减少数组-JavaScript
在数学中,严格增加的函数是要绘制的值始终增加的函数。同样,严格减小的函数是要绘制的值始终减小的函数。
我们需要编写一个JavaScript函数,该函数接受一个数字数组,如果严格增加或严格减少,则返回true,否则返回false。
示例
以下是代码-
const arr = [12, 45, 6, 4, 23, 23, 21, 1];
const arr2 = [12, 45, 67, 89, 123, 144, 2656, 5657];
const sameSlope = (a, b, c) => (b - a < 0 && c - b < 0) || (b - a > 0 && c
- b > 0);
const increasingOrDecreasing = (arr = []) => {
if(arr.length <= 2){
return true;
};
for(let i = 1; i < arr.length - 1; i++){
if(sameSlope(arr[i-1], arr[i], arr[i+1])){
continue;
};
return false;
};
return true;
};
console.log(increasingOrDecreasing(arr));
console.log(increasingOrDecreasing(arr2));输出结果
以下是控制台中的输出-
false true
热门推荐
10 新年门口花束祝福语简短
11 盘锦结婚祝福语大全简短
12 父母生日祝福语 简短独特
13 家庭恩爱祝福语简短英文
14 高考俄语祝福语大全简短
15 虎年祝福语 诗句唯美简短
16 生日婚礼祝福语简短精辟
17 虎年喝酒拜年祝福语简短
18 教师闺蜜祝福语简短