严格增加或减少数组-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
热门推荐
3 祝福语简短的微信
10 幼儿祝福语大全简短贺卡
11 感谢教官文案祝福语简短
12 鼠年同事祝福语简短创意
13 元旦祝福语 简短独特群发
14 妈咪跟我生日祝福语简短
15 给儿子新婚祝福语简短
16 伴娘祝福语长文案简短
17 恭喜高考完祝福语简短
18 生日祝福语送老公简短