仅包含严格递增数字的最长子数组 JavaScript
javascriptweb developmentfront end technologyobject oriented programming
我们需要编写一个 JavaScript 函数,该函数将数字数组作为第一个也是唯一的参数。
然后,该函数应从仅包含严格递增顺序元素的数组中返回最长连续子数组的长度。
严格递增序列是任何后续元素都大于其所有前面元素的序列。
示例
const arr = [5, 7, 8, 12, 4, 56, 6, 54, 89];
const findLongest = (arr) => {
if(arr.length == 0) {
return 0;
};
let max = 0;
let count = 0;
for(let i = 1; i < arr.length; i++) {
if(arr[i] > arr[i-1]) {
count++; }
else {
count = 0;
}
if(count > max) {
max = count;
}
}
return max + 1;
};
console.log(findLongest(arr));
输出
控制台中的输出将是 −
4
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

