严格增加或减少数组-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 妈妈生日祝福语简短励志